gpt4 book ai didi

c# - 使用反射查找 DynamicMethods

转载 作者:行者123 更新时间:2023-11-30 17:15:50 24 4
gpt4 key购买 nike

我想以某种方式找到我当前上下文中的所有 DynamicMethods,考虑我有以下方法和委托(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);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Div);
il.Emit(OpCodes.Ret);

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

return divideIt;
}

如果我要这样做:var divideIt = CreateInvoker();,我能否以某种方式使用反射来找到动态方法方法?

根据 MSDN,上面的动态方法将是静态的,一旦不再使用,它​​将由 GC 处理,我只是用它来玩反射。

我已经尝试获取正在执行的程序集中的所有类型并列出它们的所有方法,但我在 DynamicMethod 上找不到任何内容。

有什么想法吗?

最佳答案

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

关于c# - 使用反射查找 DynamicMethods,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7744161/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com