Tag: methodinfo

如何用reflection调用generics扩展方法?

我写了扩展方法GenericExtension 。 现在我想调用扩展方法Extension 。 但methodInfo的值始终为null。 public static class MyClass { public static void GenericExtension(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[] […]

如何确定ParameterInfo是否为generics类型?

我有一个GenericMethodDefinition的MethodInfo 。 如: CallMethod(T arg, string arg2) 。 GetParameters()方法将为我提供两个ParameterInfo对象,第一个是通用的,第二个不是。 如何让ParameterInfo告诉我它是通用的? 如果有约束怎么办?

检索在Func中执行的调用方法的名称

我想获得被委派为Func的方法的名称。 Func func = x => x.DoSomeMethod(); string name = ExtractMethodName(func); // should equal “DoSomeMethod” 我怎样才能做到这一点? – 吹牛的权利 – Make ExtractMethodName也可以使用属性调用,让它返回该实例中的属性名称。 例如。 Func func = x => x.Property; string name = ExtractMethodName(func); // should equal “Property”

MethodInfo调用方法

我想调用具有某个属性的方法。 所以我循环遍历所有程序集和所有方法以使用我的属性查找方法。 工作正常,但是当我得到它的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 certain method } } } 问题是我不知道包含该特定方法的类的实例。 所以我无法正确调用它,因为这些方法不是静态的。 我还想避免在可能的情况下创建此类的新实例。

如何使用reflection来调用具有特定自定义属性的所有方法?

我有一个有很多方法的课。 其中一些方法由自定义属性标记。 我想立即调用所有这些方法。 我如何使用reflection来查找该类中包含此属性的所有方法的列表?

从方法引用C#获取methodinfo

当我们想要获取指定类型的Type实例时,我们可以使用C# typeof关键字。 但是,如果我想通过它的引用获取方法的MethodInfo ,我可以使用什么? 例如,我有一个简单的控制台应用程序。 它包含Program.Main方法。 我想通过使用methodinfoof(Program.Main)类的东西获得MethodInfo 。 我有这个问题,因为方法名称可能会更改,所以我不能只使用Type.GetMethodInfo(string MethodName) 。 我有大约10000个方法,我想获得MethodInfo ,所以在我的方法中添加任何自定义属性或其他任何东西都不是解决方案。

检查针对委托的MethodInfo

如何确定MethodInfo是否适合不同的委托类型? bool IsMyDelegate(MethodInfo method); 编辑:我给了一个MethodInfo对象,想知道它是否适合委托接口。 除了明显的 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(bool)) { result = true; } else { m_Log.Error(“Validator:IsValidationDelegate”, “Method […] is not a ValidationDelegate.”); } return result; }

使用Reflection通过签名调用对象实例上的generics方法:SomeObject.SomeGenericInstanceMethod (T参数)

如何调用SomeObject.SomeGenericInstanceMethod(T arg) ? 有一些关于调用generics方法的post,但不完全像这个。 问题是method参数被约束为generics参数。 我知道如果签名是相反的 SomeObject.SomeGenericInstanceMethod(string arg) 然后我可以得到MethodInfo typeof (SomeObject).GetMethod(“SomeGenericInstanceMethod”, new Type[]{typeof (string)}).MakeGenericMethod(typeof(GenericParameter)) 那么,当常规参数是generics类型时,如何获取MethodInfo? 谢谢! 此外,generics参数可能有也可能没有类型约束。

如何获取接口方法的MethodInfo,实现类方法的MethodInfo?

我有一个接口方法的MethodInfo和一个实现接口的类的 Type 。 我想找到实现接口方法的类方法的MethodInfo 。 简单的方法method.GetBaseDefinition()不适用于接口方法。 按名称查找也不起作用,因为在显式实现接口方法时,它可以有任何名称(是的,不在C#中)。 那么正确的做法是什么,涵盖所有可能性?