typeof当我们想要获取指定类型的Type实例时,我们可以使用C#关键字.但是,如果我想MethodInfo通过它的参考得到一个方法,我可以使用什么?
例如,我有一个简单的控制台应用程序.它包含Program.Main方法.我希望MethodInfo通过使用类似的东西methodinfoof(Program.Main).我有这个问题,因为方法名称可能会改变,所以我不能只使用Type.GetMethodInfo(string MethodName)它.
我有大约10 000种方法可供我使用MethodInfo,因此在我的方法中添加任何自定义属性或其他任何方法都不是解决方案.
作为我的应用程序的一部分,我有一个接收MethodInfo的函数,需要对它进行特定的操作,这取决于该方法是否为"扩展方法".
我检查了MethodInfo类,我找不到任何显示该方法是扩展名的IsExtension属性或标志.
有谁知道如何从方法的MethodInfo中找到它?
如何(int)从a 获得返回值methodInfo.invoke?
让我感到困难的是我使用字符串变量来调用方法.
检查以下示例:
if (Convert.ToBoolean(getParameterFromXML("issue", k, 1)) == true)
{
m = k + 1;
MethodInfo methodInfo = typeof(frmDetails).GetMethod("Issue" + m);
methodInfo.Invoke(this, Parameters);
}
Run Code Online (Sandbox Code Playgroud)
我能做什么?任何帮助,将不胜感激.
特定
public Class Example
{
public static void Foo< T>(int ID){}
public static void Foo< T,U>(int ID){}
}
Run Code Online (Sandbox Code Playgroud)
问题:
如何在创建MethodInfo对象时指定任一方法?
Type exampleType = Type.GetType("fullyqualifiednameOfExample, namespaceOfExample");
MethodInfo mi = exampleType.GetMethod("Foo", BindingFlags.Public|BindingFlags.Static, null, new Type[] {typeof(Type), typeof(Type) }, null);
Run Code Online (Sandbox Code Playgroud)参数4使编译器非常不满
我正在尝试确定我从类型实例上的GetMethod调用获得的MethodInfo对象是由类型还是由它的基类实现的.
例如:
Foo foo = new Foo();
MethodInfo methodInfo = foo.GetType().GetMethod("ToString",BindingFlags|Instance);
Run Code Online (Sandbox Code Playgroud)
ToString方法可以在Foo类中实现.我想知道我是否得到了foo实现?
相关问题
我写了扩展方法GenericExtension.现在我想调用扩展方法Extension.但是值methodInfo始终为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().Extension("", typeof (int));
}
}
Run Code Online (Sandbox Code Playgroud)
怎么了?
我在这里创建了一个名为AAtribute的自定义属性,例如一个名为B的类,其中一个或多个方法使用该属性.是否有可能获得保存属性(在本例中为BMethod1)的方法的MethodInfo作为(其中一个)属性而不遍历整个程序集并查看其属性的所有已定义方法?它们是其他AttributeTargets(参数/类型/属性/ ...)的模拟方式吗?我不想要使用这种类型的属性的所有方法的数组,而只需要使用此Attirbute对象的方法.我想用它来对方法添加额外的约束(返回类型,参数,名称,其他属性用法......).
[AttributeUsage(AttributeTargets.Method)]
public class AAtribute : Attribute {
//some fields and properties
public AAtribute () {//perhaps with some parameters
//some operations
MethodInfo mi;//acces to the MethodInfo with this Attribute
//as an Attribute (the question)
//some operations with the MethodInfo
}
//some methods
}
public class B {
//some fields, properties and constructors
[A]
public void BMethod1 () {
//some operations
}
//other methods
}
Run Code Online (Sandbox Code Playgroud) 我需要使用C#通过反射调用类型的方法.
在运行时,我的数据将包含一个包含名称/值对的Dictionary.字典中的名称将对应于我将调用的方法上的参数名称.此外,在运行时,我将有一个任意的程序集限定类型名称和方法名称.在设计时,我不知道类型和方法,除了该方法将接受int,string,DateTime,bool,int [],string [],DateTime []或bool类型的可变数量的参数[].
我没有问题,我可以使用反射创建该类型的实例并调用该方法.当我调用时,我不得不将字典中的字符串值转换为方法所需的相应类型:
someMethodInfo.Invoke(instance, new [] { ... })
Run Code Online (Sandbox Code Playgroud)
我知道我需要通过MethodInfo.GetParameters()枚举并为每个参数执行类型转换.我想弄清楚的是如何做到这一点,理想情况下,如何有效地做到这一点.
到目前为止,我的研究涉及深入研究MVC源代码,因为它在将表单值传递给ActionMethod时做了类似的事情.我找到了ActionMethodDispatcher,但它使用了LINQ表达式,我对此并不熟悉.
我也在SO上看了类似的问题,但没有找到任何能回答我问题的问题.
我欢迎任何解决方案的指示.
如何从IMethodSymbol(Roslyn语法树)可靠地获取MethodInfo(反射).我可以从IMethodSymbol获取Type,并且该类型有许多方法,其中一个方法与IMethodSymbol实例匹配.
例如
int i = 0;
i.Equals(5);
Run Code Online (Sandbox Code Playgroud)
IMethodSymbol识别'等于'的地方
注意,[Int32]类型有2个[Equals]方法,一个采用[Object],另一个采用[Int32]参数.
我正在解析脚本,我没有任何工作区实例.
有任何想法吗?斯特沃
methodinfo ×10
c# ×9
reflection ×7
.net ×3
generics ×2
attributes ×1
dynamic ×1
invoke ×1
methodbase ×1
overriding ×1
roslyn ×1
typeof ×1