FieldInfo field = fieldof(string.Empty);
MethodInfo method1 = methodof(int.ToString);
MethodInfo method2 = methodof(int.ToString(IFormatProvider));
fieldof can be compiled into IL, as follows:
ldtoken
call FieldInfo.GetFieldFromHandle
methodof can be compiled into IL as:
ldtoken
call MethodBase.GetMethodFromHandle
Whenever you use the typeof operator, you can get the perfect search result for all references. Unfortunately, once you go to the field or method, you will eventually encounter Annoying hacking. I think you can do the following…or you can go back and name it.
public static FieldInfo fieldof(Expression > expression)
{
MemberExpression body = (MemberExpression)expression.Body;
return (FieldInfo)body.Member;
}
public static MethodInfo methodof(Expression > expression)
{
MethodCallExpression body = (MethodCallExpression)expression.Body;
return body.Method;
}
public static MethodInfo methodof(Expressione xpression)
{
MethodCallExpression body = (MethodCallExpression)expression.Body;
return body.Method;
}
public static void Test()< br />{
FieldInfo field = fieldof(() => string.Empty);
MethodInfo method1 = methodof(() => default(string).ToString());
MethodInfo method2 = methodof(() => default(string).ToString(default(IFormatProvider)));
MethodInfo method3 = methodof(() => default(List).Add(default(int)) );
}
It’s an awesome feature that pretty much everyone involved in the design process wishes we could do, but there are good practical reasons why we choose not to. If there comes a day when designing it and implementing it is the best way we could spend our limited budget, we’ll do it. Until then, use Reflection.
blockquote>
They can be used as follows:
FieldInfo field = fieldof(string.Empty);
MethodInfo method1 = methodof(int.ToString);
MethodInfo method2 = methodof( int.ToString(IFormatProvider));
fieldof can be compiled into IL, as follows:
ldtoken
call FieldInfo.GetFieldFromHandle< /pre>methodof can be compiled into IL as:
ldtoken
call MethodBase.GetMethodFromHandleWhenever typeof is used Operator, you can get the perfect search results for all references. Unfortunately, once you go to the field or method, you will eventually encounter annoying hacking. I think you can do the following...or you You can go back and name it by name.
public static FieldInfo fieldof(Expression > expression)
{
MemberExpression body = ( MemberExpression)expression.Body;
return (FieldInfo)body.Member;
}
public static MethodInfo methodof(Expression > expression)
{
MethodCallExpression body = (MethodCallExpression)expression.Body;
return body.Method;
}
public static MethodInfo methodof(Expressionexpression)
{
MethodCallExpression b ody = (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).Add(default(int)));
}< /p>
Eric Lippert (about the C# design team) has a good overview/discussion on this topic here. Quote:
It's an awesome feature that pretty much everyone involved in the design process wishes we could do, but there are good practical reasons why we choose not to. If there comes a day when designing it and implementing it is the best way we could spend our limited budget, we'll do it. Until then, use Reflection.