为什么会出现没有在C#`fieldof`或`methodof`运营商? [英] Why is there not a `fieldof` or `methodof` operator in C#?
问题描述
它们可以被使用如下:
字段信息字段= fieldof(的String.Empty);
MethodInfo的方法1 =推法(int.ToString);
方法2 MethodInfo的=推法(int.ToString(的IFormatProvider));
fieldof
可以被编译成IL为:
ldtoken<场及GT;
调用FieldInfo.GetFieldFromHandle
推法
可以被编译成IL为:
ldtoken<方法>
调用MethodBase.GetMethodFromHandle
每当的typeof
运算符时,你会得到完美的查找所有引用的结果。不幸的是,只要你去字段或方法,你结束了讨厌的黑客。我觉得你可以做以下...或者你可以回去通过名称获得一个字段。
公共静态字段信息fieldof< T>(防爆pression<&Func键LT; T>>前pression)
{
MemberEx pression体=(MemberEx pression)前pression.Body;
返回(字段信息)body.Member;
}公共静态的MethodInfo推法< T>(防爆pression<&Func键LT; T>>前pression)
{
MethodCallEx pression体=(MethodCallEx pression)前pression.Body;
返回body.Method;
}公共静态的MethodInfo推法(前pression<作用>前pression)
{
MethodCallEx pression体=(MethodCallEx pression)前pression.Body;
返回body.Method;
}公共静态无效测试()
{
字段信息字段= fieldof(()=>的String.Empty);
MethodInfo的方法1 =推法(()=>默认值(串)的ToString());
方法2 MethodInfo的=推法(()=>默认值(串)的ToString(默认值(的IFormatProvider)));
MethodInfo的method3 =推法(()=>默认值(表< INT方式>)加(默认值(INT)));
}
埃里克利珀(C#的设计团队)有一个很好的概述/关于这一主题<讨论href=\"http://blogs.msdn.com/ericlippert/archive/2009/05/21/in-foof-we-trust-a-dialogue.aspx\">here.引用:
这是一个真棒功能,$参与设计进程希望我们可以做p $ ptty多少每个人,但也有我们为什么不选择良好的实际原因。如果它的设计和实现它,我们可以花我们的预算有限的最好方式,当一天到来时,我们会做到这一点。在此之前,使用反射。
块引用>They could be used as follows:
FieldInfo field = fieldof(string.Empty); MethodInfo method1 = methodof(int.ToString); MethodInfo method2 = methodof(int.ToString(IFormatProvider));
fieldof
could be compiled to IL as:ldtoken <field> call FieldInfo.GetFieldFromHandle
methodof
could be compiled to IL as:ldtoken <method> call MethodBase.GetMethodFromHandle
Whenever the
typeof
operator is used, you get perfect Find All References results. Unfortunately, as soon as you go to fields or methods, you end up with nasty hacks. I think you could do the following... or you can go back to getting a field by name.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))); }
解决方案Eric Lippert (on the C# design team) has an excellent overview/discussion on this topic here. To 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.
这篇关于为什么会出现没有在C#`fieldof`或`methodof`运营商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!