Home » .Net Framework

MethodOf keyword? Token instead of string for method in compiled code

I was looking at some code in reflector today and I noticed something interesting. I saw the use of some methodof keyword.

Expression in C#

Expression<Action<int>> expression = (value) => DoSomething(value);

Expression in Reflector

   Expression<Action<int>> expression = Expression.Lambda<Action<int>>(Expression.Call(null, (MethodInfo) methodof (SemiDomainMapper.DoSomething), new Expression[] { CS$0$0001 = Expression.Parameter(typeof(int), "value") }), new ParameterExpression[] { CS$0$0001 });

Expression in IL

    L_003a: ldtoken int32
    L_003f: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
    L_0044: ldstr "value"
    L_0049: call class [System.Core]System.Linq.Expressions.ParameterExpression [System.Core]System.Linq.Expressions.Expression::Parameter(class [mscorlib]System.Type, string)
    L_004e: stloc.s CS$0$0001
    L_0050: ldnull
    L_0051: ldtoken void EnvironmentSyncService.SemiDomainMapper::DoSomething(int32)
    L_0056: call class [mscorlib]System.Reflection.MethodBase [mscorlib]System.Reflection.MethodBase::GetMethodFromHandle(valuetype [mscorlib]System.RuntimeMethodHandle)
    L_005b: castclass [mscorlib]System.Reflection.MethodInfo

Looks like reflector thinks when we use ldtoken followed by a GetMethodFromHandle we are using the methodof keyword?  As far as I know there is no way to do this from C# code?





5 Answers Found


Answer 1


Currently, there is no methodof operator in C#, Eric Lippert has a blog on this topic.


Answer 2

Okay.  So the csc creates IL that cannot be represented in C# when creating expressions.  The people that make reflector made up this methodof keyword  so we can view it in their tool?

Answer 3


Have to say, many developers wants the operator, but currently it is an invalid keyword  in C#,  the Reflector tool just involves it in advance. As Daniel said in his blog:


... It's not a valid C# keyword, but given that Lutz Roeder works for Microsoft (not on the C# team, though), let's hope it will get eventually in the language :).


That methodof translation is actually the conversion from the following two IL lines:


L_0017: ldtoken instance object [mscorlib]System.ICloneable::Clone()L_001c: call  class [mscorlib]System.Reflection.MethodBase [mscorlib]System.Reflection.MethodBase::GetMethodFromHandle(valuetype [mscorlib]System.RuntimeMethodHandle)




MSDN Subscriber Support in Forum


Answer 4



I'm writing to check the issue status, please feel free to let us know if you have any concern.


Answer 5


We temporarily mark a reply,please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.



<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure