它们可以如下使用:
FieldInfo field = fieldof(string.Empty);
MethodInfo method1 = methodof(int.ToString);
MethodInfo method2 = methodof(int.ToString(IFormatProvider));
Run Code Online (Sandbox Code Playgroud)
fieldof 可以编译为IL为:
ldtoken <field>
call FieldInfo.GetFieldFromHandle
Run Code Online (Sandbox Code Playgroud)
methodof 可以编译为IL为:
ldtoken <method>
call MethodBase.GetMethodFromHandle
Run Code Online (Sandbox Code Playgroud)
无论何时使用typeof运算符,您都可以获得完美的查找所有引用结果.不幸的是,一旦你去了田野或方法,你最终会遇到令人讨厌的黑客攻击.我想你可以做以下事情......或者你可以回去按名字命名.
public static FieldInfo fieldof<T>(Expression<Func<T>> expression)
{
MemberExpression body = (MemberExpression)expression.Body;
return (FieldInfo)body.Member;
}
public static MethodInfo methodof<T>(Expression<Func<T>> expression)
{
MethodCallExpression body = (MethodCallExpression)expression.Body;
return body.Method;
}
public static MethodInfo methodof(Expression<Action> expression)
{
MethodCallExpression body = (MethodCallExpression)expression.Body;
return body.Method;
}
public static void Test()
{
FieldInfo field = fieldof(() => string.Empty);
MethodInfo method1 = methodof(() => default(string).ToString());
MethodInfo method2 = methodof(() => default(string).ToString(default(IFormatProvider)));
MethodInfo method3 = methodof(() => default(List<int>).Add(default(int)));
}
Run Code Online (Sandbox Code Playgroud)
Mag*_*tLU 35
实际上,您可以避免同时使用反射和lambdas(.NET Framework 2.0).考虑以下课程:
public class methodof<T>
{
private MethodInfo method;
public methodof(T func)
{
Delegate del = (Delegate)(object)func;
this.method = del.Method;
}
public static implicit operator methodof<T>(T methodof)
{
return new methodof<T>(methodof);
}
public static implicit operator MethodInfo(methodof<T> methodof)
{
return methodof.method;
}
}
Run Code Online (Sandbox Code Playgroud)
它的用法:
MethodInfo writeln = (methodof<Action>)Console.WriteLine;
MethodInfo parse = (methodof<Func<string, int>>)int.Parse;
Run Code Online (Sandbox Code Playgroud)
@ 280Z28 - 当我找到你的问题和代码时,我们只是坐下来弄清楚如何做到这一点.我们需要一个PropertyOf方法,所以我添加了它.在这里,万一其他人需要它.感谢这个伟大的问题.
public static PropertyInfo PropertyOf<T>(Expression<Func<T>> expression)
{
MemberExpression body = (MemberExpression)expression.Body;
PropertyInfo pi = body.Member as PropertyInfo;
if (pi != null)
{
return pi;
}
else throw new ArgumentException("Lambda must be a Property.");
}
[TestMethod()]
public void MethodofPropertyOfTest<T>()
{
string foo = "Jamming";
MethodInfo method1 = ReflectionHelper.Methodof(() => default(string).ToString());
PropertyInfo prop = ReflectionHelper.PropertyOf(() => default(string).Length);
Assert.AreEqual(method1.Invoke(foo, null), "Jamming");
Assert.AreEqual(prop.GetGetMethod().Invoke(foo, null), foo.Length);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6629 次 |
| 最近记录: |