我在这里创建了一个名为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时,如何调用某个方法.
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我不知道包含该特定方法的类的实例.所以我无法正确调用它,因为这些方法不是静态的.我还想避免在可能的情况下创建此类的新实例.
我有这个小问题,我无法弄清楚要传递给Type.GetMethod的哪些参数,以便在非泛型类型上返回泛型方法的MethodInfo.具体来说,我有这种类型的定义:
public static class A
{
public static B F<T>(bool dummy)
{
}
public static B F<T>(IEnumerable<T> arg)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
我在Type.GetMethod上尝试了几次,但没有人会返回F方法的MethodInfo.
我知道我可以调用Type.GetMethods甚至Type.FindMember,但我对Type.GetMethod感兴趣.
有任何想法吗?
谢谢.
编辑
实际上,我的代码有点复杂.泛型方法被重载,因此我不能仅使用函数名称来使用Type.GetMethod.我试过这些变种:
typeof(A).GetMethod("F", BindingFlags.Static | BindingFlags.Public, null, new Type[]{ typeof(IEnumerable<>) }, null)
typeof(A).GetMethod("F`1", BindingFlags.Static | BindingFlags.Public, null, new Type[]{ typeof(IEnumerable<>) }, null)
typeof(A).GetMethod("F[T]", BindingFlags.Static | BindingFlags.Public, null, new Type[]{ typeof(IEnumerable<>) }, null)
typeof(A).GetMethod("F[[T]]", BindingFlags.Static | BindingFlags.Public, null, new Type[]{ typeof(IEnumerable<>) }, null)
Run Code Online (Sandbox Code Playgroud) 我有MethodInfo
一个GenericMethodDefinition.如:CallMethod<T>(T arg, string arg2)
.GetParameters()方法将为我提供两个ParameterInfo对象,第一个是通用的,第二个不是.如何让ParameterInfo告诉我它是通用的?如果它有约束怎么办?
如果MethodInfo接受参数,那么询问MethodInfo的最有效方法是什么?如果接受,有多少?
我目前的解决方案是:methodInfo.GetParameters().Any()
和methodInfo.GetParameters().Count()
.
这是最有效的方式吗?
由于我实际上并不需要任何ParameterInfo
对象,有没有办法在没有调用的情况下执行此操作GetParameters()
?
我有一个有很多方法的课.
其中一些方法由自定义属性标记.
我想立即调用所有这些方法.
我如何使用反射来查找该类中包含此属性的所有方法的列表?
现在,我有: targetType.GetMethod("get_Item", BindingFlags.Instance)
有更好的吗?
这是我的测试代码:扩展方法GetInstructions
来自此处:https : //gist.github.com/jbevain/104001
using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
typeof(TestClass)
.GetMethods()
.Where(method => method.Name == "Say" || method.Name == "Hello")
.ToList()
.ForEach(method =>
{
var calls = method.GetInstructions()
.Select(x => x.Operand as MethodInfo)
.Where(x => x != null)
.ToList();
Console.WriteLine(method);
calls.ForEach(call =>
{
Console.WriteLine($"\t{call}");
call.GetGenericArguments().ToList().ForEach(arg => Console.WriteLine($"\t\t{arg.FullName}"));
});
});
Console.ReadLine();
}
}
class TestClass
{
public async Task Say()
{
await HelloWorld.Say<IFoo>();
HelloWorld.Hello<IBar>();
} …
Run Code Online (Sandbox Code Playgroud) methodinfo ×10
c# ×9
reflection ×9
.net ×2
attributes ×2
dynamic ×1
generics ×1
invoke ×1
items ×1
performance ×1
roslyn ×1