methodinfo

Use Reflection to call generic method on object instance with signature: SomeObject.SomeGenericInstanceMethod<T>(T argument)

天大地大妈咪最大 提交于 2019-11-28 02:10:51
How do I call SomeObject.SomeGenericInstanceMethod<T>(T arg) ? There are a few posts about calling generic methods, but not quite like this one. The problem is that the method argument parameter is constrained to the generic parameter. I know that if the signature were instead SomeObject.SomeGenericInstanceMethod<T>(string arg) then I could get the MethodInfo with typeof (SomeObject).GetMethod("SomeGenericInstanceMethod", new Type[]{typeof (string)}).MakeGenericMethod(typeof(GenericParameter)) So, How do I go about getting the MethodInfo when the regular arguments are of a generic type? Thanks

Retrieving the name of the invoked method executed in a Func

一笑奈何 提交于 2019-11-28 02:03:46
问题 I would like to get the name of the method that is being delegated as a Func. Func<MyObject, object> func = x => x.DoSomeMethod(); string name = ExtractMethodName(func); // should equal "DoSomeMethod" How can I achieve this? -- For bragging rights -- Make ExtractMethodName also work with a property invocation, having it return the property name in that instance. eg. Func<MyObject, object> func = x => x.Property; string name = ExtractMethodName(func); // should equal "Property" 回答1: Look Ma!

How to call a generic extension method with reflection?

此生再无相见时 提交于 2019-11-28 01:51:50
I wrote the extension method GenericExtension . Now I want to call the extension method Extension . But the value of methodInfo is always null. public static class MyClass { public static void GenericExtension<T>(this Form a, string b) where T : Form { // code... } public static void Extension(this Form a, string b, Type c) { MethodInfo methodInfo = typeof(Form).GetMethod("GenericExtension", new[] { typeof(string) }); MethodInfo methodInfoGeneric = methodInfo.MakeGenericMethod(new[] { c }); methodInfoGeneric.Invoke(a, new object[] { a, b }); } private static void Main(string[] args) { new Form

Invoke method by MethodInfo

我的未来我决定 提交于 2019-11-28 00:39:52
问题 I want to invoke methods with a certain attribute. So I'm cycling through all the assemblies and all methods to find the methods with my attribute. Works fine, but how do I invoke a certain method when I only got it's MethodInfo. AppDomain app = AppDomain.CurrentDomain; Assembly[] ass = app.GetAssemblies(); Type[] types; foreach (Assembly a in ass) { types = a.GetTypes(); foreach (Type t in types) { MethodInfo[] methods = t.GetMethods(); foreach (MethodInfo method in methods) { // Invoke a

How would I use reflection to call all the methods that has a certain custom attribute?

空扰寡人 提交于 2019-11-27 22:30:55
问题 I have a class with a bunch of methods. some of these methods are marked by a custom attribute. I would like to call all these methods at once. How would I go about using reflection to find a list of all the methods in that class that contains this attribute? 回答1: Once you get the list of methods, you would cycle query for the custom attributes using the GetCustomAttributes method. You may need to change the BindingFlags to suit your situation. var methods = typeof( MyClass ).GetMethods(

How can I get fields used in a method (.NET)?

浪尽此生 提交于 2019-11-27 21:55:33
问题 In .NET, using reflection how can I get class variables that are used in a method? Ex: class A { UltraClass B = new(..); SupaClass C = new(..); void M1() { B.xyz(); // it can be a method call int a = C.a; // a variable access } } Note: GetClassVariablesInMethod(M1 MethodInfo) returns B and C variables. By variables I mean Value and/or Type and Constructor Parameters of that specific variable. 回答1: There's a lot of different answers, but as not a single one appeals to me, here's mine. It's

How can I create an Action delegate from MethodInfo?

亡梦爱人 提交于 2019-11-27 20:23:58
I want to get an action delegate from a MethodInfo object. Is this possible? 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 method Action action = (Action) method.CreateDelegate(typeof(Action)); // Instance method (on "target") Action

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

只谈情不闲聊 提交于 2019-11-27 11:01:01
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. Let's say I don't know the above class is called MyClass ; I've got some object, x , but I have no idea

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

爱⌒轻易说出口 提交于 2019-11-27 07:46:46
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. You need to create the argument array first, and keep a reference to it. The out parameter value will then be stored in the array. So you can use: object[] arguments = new object[] { "test",

How to call a generic extension method with reflection?

瘦欲@ 提交于 2019-11-27 04:49:42
问题 I wrote the extension method GenericExtension . Now I want to call the extension method Extension . But the value of methodInfo is always null. public static class MyClass { public static void GenericExtension<T>(this Form a, string b) where T : Form { // code... } public static void Extension(this Form a, string b, Type c) { MethodInfo methodInfo = typeof(Form).GetMethod("GenericExtension", new[] { typeof(string) }); MethodInfo methodInfoGeneric = methodInfo.MakeGenericMethod(new[] { c });