相关疑难解决方法(0)

获取实现接口的所有类型

使用反射,如何使用最少的代码获得使用C#3.0/.NET 3.5实现接口的所有类型,并最大限度地减少迭代?

这就是我想要重写的内容:

foreach (Type t in this.GetType().Assembly.GetTypes())
    if (t is IMyInterface)
        ; //do stuff
Run Code Online (Sandbox Code Playgroud)

c# reflection optimization lambda c#-3.0

524
推荐指数
11
解决办法
24万
查看次数

在所有装配中查找类型

我需要在网站或Windows应用程序中的所有程序集中查找特定类型,是否有一种简单的方法可以执行此操作?就像ASP.NET MVC的控制器工厂如何查看控制器的所有程序集一样.

谢谢.

.net c# asp.net reflection types

59
推荐指数
4
解决办法
5万
查看次数

使用反射查找具有自定义属性的方法

我有一个自定义属性:

public class MenuItemAttribute : Attribute
{
}
Run Code Online (Sandbox Code Playgroud)

和一个有几个方法的类:

public class HelloWorld
{
    [MenuItemAttribute]
    public void Shout()
    {
    }

    [MenuItemAttribute]
    public void Cry()
    {
    }

    public void RunLikeHell()
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

如何只获取使用自定义属性修饰的方法?

到目前为止,我有这个:

string assemblyName = fileInfo.FullName;
byte[] assemblyBytes = File.ReadAllBytes(assemblyName);
Assembly assembly = Assembly.Load(assemblyBytes);

foreach (Type type in assembly.GetTypes())
{
     System.Attribute[] attributes = System.Attribute.GetCustomAttributes(type);

     foreach (Attribute attribute in attributes)
     {
         if (attribute is MenuItemAttribute)
         {
             //Get me the method info
             //MethodInfo[] methods = attribute.GetType().GetMethods();
         }
     }
}
Run Code Online (Sandbox Code Playgroud)

我现在需要的是获取方法名称,返回类型以及它接受的参数.

c# reflection custom-attributes

48
推荐指数
3
解决办法
5万
查看次数

使用反射获取类方法

当类名作为字符串传递时,如何使用反射获取类的所有公共方法,如下面的方法所示.?

 private  MethodInfo[] GetObjectMethods(string selectedObjClass)
 {
   MethodInfo[] methodInfos;
   Assembly assembly = Assembly.GetAssembly(typeof(sampleAdapater));
   Type _type = assembly.GetType("SampleSolution.Data.MyData." + selectedObjClass);

  ///get all the methods for the classname passed as string

   return methodInfos;

 }
Run Code Online (Sandbox Code Playgroud)

请帮忙.谢谢

c# reflection

37
推荐指数
2
解决办法
5万
查看次数