methodinfo

How to get MethodInfo of interface method, having implementing MethodInfo of class method?

ぃ、小莉子 提交于 2019-11-27 04:46:46
I have a MethodInfo of an interface method and Type of a class that implements the interface . I want to find the MethodInfo of the class method that implements the interface method. The simple method.GetBaseDefinition() does not work with interface methods. Lookup by name won't work either, because when implementing interface method explicitly it can have any name (yes, not in C#). So what is the correct way of doing that that covers all the possibilities? OK, I found a way, using GetInterfaceMap . var map = targetType.GetInterfaceMap(interfaceMethod.DeclaringType); var index = Array.IndexOf

Checking a MethodInfo against a delegate

谁说胖子不能爱 提交于 2019-11-27 02:50:42
问题 How can I determine if a MethodInfo fits a distinct Delegate Type? bool IsMyDelegate(MethodInfo method); Edit: I'm given a MethodInfo object and want to know if it fits the delegate interface. Apart from the obvious private bool IsValidationDelegate(MethodInfo method) { var result = false; var parameters = method.GetParameters(); if (parameters.Length == 2 && parameters[0].ParameterType == typeof(MyObject1) && parameters[1].ParameterType == typeof(MyObject2) && method.ReturnType == typeof

Using reflection to check if a method is “Extension Method”

烈酒焚心 提交于 2019-11-27 02:39:07
问题 As part of my application I have a function that receives a MethodInfo and need to do specific operations on it depending if that method is "Extension Method". I've checked the MethodInfo class and I could not find any IsExtension property or flag that shows that the method is extension. Does anyone knows how can I find that from the method's MethodInfo? 回答1: Based on F# extension methods in C# it seems there is an attribute on the compiled form. So see if the method has this attribute: http:

How can I create an Action delegate from MethodInfo?

北城以北 提交于 2019-11-26 20:19:33
问题 I want to get an action delegate from a MethodInfo object. Is this possible? 回答1: Use Delegate.CreateDelegate: // Static method Action action = (Action) Delegate.CreateDelegate(typeof(Action), method); // Instance method (on "target") Action action = (Action) Delegate.CreateDelegate(typeof(Action), target, method); For an Action<T> etc, just specify the appropriate delegate type everywhere. In .NET Core, Delegate.CreateDelegate doesn't exist, but MethodInfo.CreateDelegate does: // Static

Can you get a Func<T> (or similar) from a MethodInfo object?

若如初见. 提交于 2019-11-26 15:23:29
问题 I realize that, generally speaking, there are performance implications of using reflection. (I myself am not a fan of reflection at all, actually; this is a purely academic question.) Suppose there exists some class that looks like this: public class MyClass { public string GetName() { return "My Name"; } } Bear with me here. I know that if I have an instance of MyClass called x , I can call x.GetName() . Furthermore, I could set a Func<string> variable to x.GetName . Now here's my question.

How to pass a parameter as a reference with MethodInfo.Invoke

天涯浪子 提交于 2019-11-26 13:48:41
问题 How can I pass a parameter as a reference with MethodInfo.Invoke ? This is the method I want to call: private static bool test(string str, out byte[] byt) I tried this but I failed: byte[] rawAsm = new byte[]{}; MethodInfo _lf = asm.GetTypes()[0].GetMethod("test", BindingFlags.Static | BindingFlags.NonPublic); bool b = (bool)_lf.Invoke(null, new object[] { "test", rawAsm }); The bytes returned are null. 回答1: You need to create the argument array first, and keep a reference to it. The out

How to get MethodInfo of interface method, having implementing MethodInfo of class method?

ぐ巨炮叔叔 提交于 2019-11-26 12:46:53
问题 I have a MethodInfo of an interface method and Type of a class that implements the interface . I want to find the MethodInfo of the class method that implements the interface method. The simple method.GetBaseDefinition() does not work with interface methods. Lookup by name won\'t work either, because when implementing interface method explicitly it can have any name (yes, not in C#). So what is the correct way of doing that that covers all the possibilities? 回答1: OK, I found a way, using