从methodinfo获取代理

编程入门 行业动态 更新时间:2024-10-26 02:29:25
本文介绍了从methodinfo获取代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个下拉列表,它通过检查一个类的方法并包括与特定签名相匹配的方法来填充。问题在于从列表中获取所选项目,并让代理在该类中调用该方法。第一种方法是有效的,但我无法确定第二种方法。

例如,

public delegate void MyDelegate(MyState state); public static MyDelegate GetMyDelegateFromString(string methodName) { switch(methodName) { caseCallMethodOne:返回MyFunctionsClass。 CallMethodOne; caseCallMethodTwo: return MyFunctionsClass.CallMethodTwo; 默认值:返回MyFunctionsClass.CallMethodOne; } } public static MyDelegate GetMyDelegateFromStringReflection(string methodName) { MyDelegate function = MyFunctionsClass.CallMethodOne; 输入inf = typeof(MyFunctionsClass); foreach(inf.GetMethods()中的var方法) { if(method.Name == methodName) { // function = method; //如何获取函数调用? } } 返回函数; }

如何获取第二种方法的注释部分?如何将 MethodInfo 投入委托?

谢谢!

编辑:这是工作解决方案。

public static MyDelegate GetMyDelegateFromStringReflection methodName) { MyDelegate function = MyFunctionsClass.CallMethodOne; 输入inf = typeof(MyFunctionsClass); foreach(inf.GetMethods()中的var方法) { if(method.Name == methodName) { function =(MyDelegate)Delegate.CreateDelegate (typeof(MyDelegate),方法); } } 返回函数; }

解决方案

你需要打电话某些形式的 Delegate.CreateDelegate(),具体取决于该方法是否为静态或实例方法。

I have a drop down list that is populated by inspecting a class's methods and including those that match a specific signature. The problem is in taking the selected item from the list and getting the delegate to call that method in the class. The first method works, but I cannot figure out part of the second.

For example,

public delegate void MyDelegate(MyState state); public static MyDelegate GetMyDelegateFromString(string methodName) { switch (methodName) { case "CallMethodOne": return MyFunctionsClass.CallMethodOne; case "CallMethodTwo": return MyFunctionsClass.CallMethodTwo; default: return MyFunctionsClass.CallMethodOne; } } public static MyDelegate GetMyDelegateFromStringReflection(string methodName) { MyDelegate function = MyFunctionsClass.CallMethodOne; Type inf = typeof(MyFunctionsClass); foreach (var method in inf.GetMethods()) { if (method.Name == methodName) { //function = method; //how do I get the function to call? } } return function; }

How do I get the commented out section of the second method to work? How do I cast the MethodInfo into the delegate?

Thanks!

Edit: Here is the working solution.

public static MyDelegate GetMyDelegateFromStringReflection(string methodName) { MyDelegate function = MyFunctionsClass.CallMethodOne; Type inf = typeof(MyFunctionsClass); foreach (var method in inf.GetMethods()) { if (method.Name == methodName) { function = (MyDelegate)Delegate.CreateDelegate(typeof(MyDelegate), method); } } return function; }

解决方案

You'll need to call some form of Delegate.CreateDelegate(), depending on whether the method in question is a static or instance method.

更多推荐

从methodinfo获取代理

本文发布于:2023-11-05 04:14:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559869.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:methodinfo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!