如何动态调用匿名对象函数 属性?
How to Invoke dynamicaly an anonymus objects function property?
我在这方面非常新,所以欢迎大家提供帮助。
所以我有这个匿名对象(不确定这是它的正确名称):
var ERRORS = new
{
ERROR = new Func<bool>(() =>{ return true; })
, ERROR1 = new Func<bool>(() => { return true; })
, ERROR2 = new Func<bool>(() => { return true; })
, ERROR3 = new Func<bool>(() => { return true; })
, ERROR4 = new Func<bool>(() => { return true; })
, ERROR5 = new Func<bool>(() => { return true; })
, ERROR6 = new Func<bool>(() => { return true; })
, ERROR7 = new Func<bool>(() => { return true; })
, ERROR8 = new Func<bool>(() => { return true; })
, ERROR9 = new Func<bool>(() => { return true; })
, ERROR10 = new Func<bool>(() => { return true; })
, ERROR11 = new Func<bool>(() => { return true; })
, ERROR12 = new Func<bool>(() => { return true; })
};
我想遍历此对象属性并像调用函数一样调用它们。
到目前为止我已经制作了这段代码:
Type type = ERRORS.GetType();
MethodInfo[] properties = type.GetMethods();
foreach (MethodInfo property in properties)
{
Delegate del = property.CreateDelegate(typeof(System.Func<bool>));
Console.WriteLine("Name: " + property.Name + ", Value: " + del.Method.Invoke(ERRORS,null));
}
这段代码是我在网上找到的,做了一些调整但抛出异常:
"Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type."
对我来说意义不大。
如前所述,我是 C# 的新手,因此我们将不胜感激。
您不能在匿名对象中创建方法。您只能拥有属性(可以是委托)...
所以:
Type type = ERRORS.GetType();
// Properties:
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
// Get the value of the property, cast it to the right type
Func<bool> func = (Func<bool>)property.GetValue(ERRORS);
// Call the delegate normally (`func()` in this case)
Console.WriteLine("Name: " + property.Name + ", Value: " + func());
}
请注意,无需反射,您可以调用以下方法:
bool res = ERRORS.ERROR1();
或
Func<bool> func = ERRORS.ERROR1();
bool res = func();
请注意,通常您所做的几乎是无用的,因为在定义它的函数之外传递匿名对象通常是错误的,而在函数内部您已经知道它的 "shape"(您知道它有哪些属性,以及它们的名称)
真的有必要使用匿名类型+反射吗?为什么不是 Func 数组?
示例:
var errors = new Func<bool> []
{
new Func<bool>(() => { return true; }),
() => { return true; },
() => { return true; },
() => { return true; },
};
errors[0](); // take delegate by index and invoke
我在这方面非常新,所以欢迎大家提供帮助。
所以我有这个匿名对象(不确定这是它的正确名称):
var ERRORS = new
{
ERROR = new Func<bool>(() =>{ return true; })
, ERROR1 = new Func<bool>(() => { return true; })
, ERROR2 = new Func<bool>(() => { return true; })
, ERROR3 = new Func<bool>(() => { return true; })
, ERROR4 = new Func<bool>(() => { return true; })
, ERROR5 = new Func<bool>(() => { return true; })
, ERROR6 = new Func<bool>(() => { return true; })
, ERROR7 = new Func<bool>(() => { return true; })
, ERROR8 = new Func<bool>(() => { return true; })
, ERROR9 = new Func<bool>(() => { return true; })
, ERROR10 = new Func<bool>(() => { return true; })
, ERROR11 = new Func<bool>(() => { return true; })
, ERROR12 = new Func<bool>(() => { return true; })
};
我想遍历此对象属性并像调用函数一样调用它们。
到目前为止我已经制作了这段代码:
Type type = ERRORS.GetType();
MethodInfo[] properties = type.GetMethods();
foreach (MethodInfo property in properties)
{
Delegate del = property.CreateDelegate(typeof(System.Func<bool>));
Console.WriteLine("Name: " + property.Name + ", Value: " + del.Method.Invoke(ERRORS,null));
}
这段代码是我在网上找到的,做了一些调整但抛出异常:
"Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type."
对我来说意义不大。
如前所述,我是 C# 的新手,因此我们将不胜感激。
您不能在匿名对象中创建方法。您只能拥有属性(可以是委托)...
所以:
Type type = ERRORS.GetType();
// Properties:
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
// Get the value of the property, cast it to the right type
Func<bool> func = (Func<bool>)property.GetValue(ERRORS);
// Call the delegate normally (`func()` in this case)
Console.WriteLine("Name: " + property.Name + ", Value: " + func());
}
请注意,无需反射,您可以调用以下方法:
bool res = ERRORS.ERROR1();
或
Func<bool> func = ERRORS.ERROR1();
bool res = func();
请注意,通常您所做的几乎是无用的,因为在定义它的函数之外传递匿名对象通常是错误的,而在函数内部您已经知道它的 "shape"(您知道它有哪些属性,以及它们的名称)
真的有必要使用匿名类型+反射吗?为什么不是 Func 数组?
示例:
var errors = new Func<bool> []
{
new Func<bool>(() => { return true; }),
() => { return true; },
() => { return true; },
() => { return true; },
};
errors[0](); // take delegate by index and invoke