Hi,

On Sun, Dec 12, 2010 at 2:59 AM, ayke <[email protected]> wrote:
> Is it possible to get the current calling MethodDefinition like with
> StackFrame (StackFrame returns MethodInfo)

To start with, you can use the current MethodBase using Reflection by
using MethodBase.GetCurrentMethod.

There's nothing in Cecil to retrieve the MethodDefinition associated
with the running method, as Cecil is not really a runtime type system,
but you can easily retrieve it:

                public static MethodDefinition ToDefinition (this SR.MethodBase 
method)
                {
                        var declaring_type = method.DeclaringType.ToDefinition 
();
                        return (MethodDefinition) 
declaring_type.Module.LookupToken
(method.MetadataToken);
                }

So that would be:

public void MyMethod ()
{
        var my_method_definition =
System.Reflection.MethodBase.GetCurrentMethod ().ToDefinition ();
}

So it's definitely possible. Now, you need to think whether or not it
is a good idea to do so in a first place. GetCurrentMethod can be
costly. If you're going to process your assembly with Cecil anyway,
what about injecting code like:

ldtoken my_method_definition
call class [mscorlib]System.Reflection.MethodBase
[mscorlib]System.Reflection.MethodBase::GetMethodFromHandle(valuetype
[mscorlib]System.RuntimeMethodHandle)
castclass  [mscorlib]System.Reflection.MethodInfo

To give you your current MethodInfo?

-- 
--
mono-cecil

Reply via email to