有没有办法将类型数组传递给“is”运算符?
我正在尝试简化针对多种类型检查对象的语法。
类似的东西:
public static function bool IsOfType(object Obj,params Type[] Types)
但是这需要以下用法:
if(X.IsOfType(typeof(int),typeof(float))
{...}
我想做点什么:
if(X is {int,float})
或
if(X.IsOfType(int,float))
甚至
public static bool ISOfType<T[]>(this object Obj){...}
if(X.ISOfType<int,float>())
我认为他们都不可能。
答案 0 :(得分:1)
它看起来很疯狂,但您可以使用以下Fluent语法:
object someInstance = 5.0;
if(someInstance
.Is<int>()
.Or<double>()) {
// ...
}
这里的fluent语法实现如下:
static class FluentIs {
public static IsResult Is<T>(this object target) {
return new IsResult(target, () => target is T);
}
public static IsResult Or<T>(this IsResult prev) {
return new IsResult(prev, (v, x) => v || (x is T));
}
public class IsResult {
Func<bool> value;
object target;
internal IsResult(IsResult r, Func<bool, object, bool> getValue) :
this(r.target, () => getValue(r.value(), r.target)) {
}
internal IsResult(object target, Func<bool> value) {
this.target = target;
this.value = value;
}
// bool Operators
public static bool operator true(IsResult r) { return r.value(); }
public static bool operator false(IsResult r) { return !r.value(); }
}
}
答案 1 :(得分:0)
如果您可以将类型作为泛型参数传递,那么就有一个解决方案。不幸的是,C#不支持可变参数泛型。您必须为每个通用arity定义函数。
public static bool IsOfType<T>(this object obj) => obj is T;
public static bool IsOfType<T1, T2>(this object obj) => obj is T1 || obj is T2;
public static bool IsOfType<T1, T2, T3>(this object obj) => obj is T1 || obj is T2 || obj is T3;
public static bool IsOfType<T1, T2, T3, T4>(this object obj) => obj is T1 || obj is T2 || obj is T3 || obj is T4;
public static bool IsOfType<T1, T2, T3, T4, T5>(this object obj) => obj is T1 || obj is T2 || obj is T3 || obj is T4 || obj is T5;
public static bool IsOfType<T1, T2, T3, T4, T5, T6>(this object obj) => obj is T1 || obj is T2 || obj is T3 || obj is T4 || obj is T5 || obj is T6;
public static bool IsOfType<T1, T2, T3, T4, T5, T6, T7>(this object obj) => obj is T1 || obj is T2 || obj is T3 || obj is T4 || obj is T5 || obj is T6 || obj is T7;
public static bool IsOfType<T1, T2, T3, T4, T5, T6, T7, T8>(this object obj) => obj is T1 || obj is T2 || obj is T3 || obj is T4 || obj is T5 || obj is T6 || obj is T7 || obj is T8;
我怀疑你需要超过8种类型,但如果你这样做,只需要定义更多的重载。
答案 2 :(得分:0)
我的“选择”解决方案是使用我方便的“In”扩展函数来根据对象类型名称进行验证。
public static bool IsAnyOf(this object Obj,params string[] TypeNames)
{
return Obj.GetType().Name.In(TypeNames);
}
public static bool In(this string Needle,params string [] Haystack)
{
foreach (string straw in Haystack)
if (straw == Needle)
return true;
return false;
}
用法:
public static void CheckAll(this TreeNode Node)
{
foreach(TreeNode Child in Node.Nodes)
{
if(Child.GetType().Name.In("ChildTablesNode","ChildTableNode"))
{
Child.Checked = Node.Checked;
Child.CheckAll();
}
}
}
private void ctxTree_Opening(object sender, CancelEventArgs e)
{
TreeNode nSelected = Tree.SelectedNode;
ctxScript.Visible = !nSelected.IsAnyOf("TablesNode"
, "ViewsNode"
, "ProceduresNode"
, "UserTableTypesNode"
, "FunctionsNode"
, "ServerNode"
, "DatabaseNode"
, "ColumnsNode"
, "ColumnNode"
, "TriggersNode");
Type[] x = { typeof(int) };
}