开发者

Using reflection to find DynamicMethods

开发者 https://www.devze.com 2023-04-12 08:55 出处:网络
I\'d like to somehow find all DynamicMethods in my current context, consider that I have the following method and delegate:

I'd like to somehow find all DynamicMethods in my current context, consider that I have the following method and delegate:

public delegate double DivideInvoker(int a, int b);
public DivideInvoker CreateInvoker()
{
    Type[] methodArguments = { 
                        typeof(int), 
                        typeof(int)
                    };

    DynamicMethod division = new DynamicMethod(
            "Division",
            typeof(double),
            methodArguments,
            typeof(MyMethodCreator));

    ILGenerator il = division.GetILGenerator();
    il.Emit(OpCodes.Ldarg_0开发者_如何学Go);
    il.Emit(OpCodes.Ldarg_1);
    il.Emit(OpCodes.Div);
    il.Emit(OpCodes.Ret);

    var divideIt = (DivideInvoker)division.CreateDelegate(typeof(DivideInvoker));

    return divideIt;
}

If I were to do: var divideIt = CreateInvoker();, can I somehow use reflection to find the dynamic method method?

According to MSDN the above dynamic method will be static and once it's not used anymore it will be disposed by the GC, I am just using this to play around with reflection.

I've tried getting all types in the executing assembly and listing all the methods on them, but I can't find anything on the DynamicMethod.

Any ideas?


DivideInvoker invoker = CreateInvoker();
MethodInfo method = invoker.Method;
// method will contain the MethodInfo of the Division dynamic method
// so you could use reflection on it
0

精彩评论

暂无评论...
验证码 换一张
取 消