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

  • Question

  • 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?

     

     

     

    Wednesday, June 9, 2010 11:52 PM

Answers

  •  

    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)

     

    Sincerely,

    Eric

    MSDN Subscriber Support in Forum

    • Marked as answer by eryang Friday, June 18, 2010 7:36 AM
    Friday, June 11, 2010 6:27 AM

All replies

  •  

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


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, June 10, 2010 9:43 AM
  • 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?
    Thursday, June 10, 2010 6:39 PM
  •  

    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)

     

    Sincerely,

    Eric

    MSDN Subscriber Support in Forum

    • Marked as answer by eryang Friday, June 18, 2010 7:36 AM
    Friday, June 11, 2010 6:27 AM
  •  

    Hi,

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


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, June 15, 2010 9:11 AM
  •  

    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.


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, June 18, 2010 7:36 AM