Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# assembly.GetTypes()和GetExportedTypes()不会返回所有公共对象_C#_Reflection_System.reflection - Fatal编程技术网

C# assembly.GetTypes()和GetExportedTypes()不会返回所有公共对象

C# assembly.GetTypes()和GetExportedTypes()不会返回所有公共对象,c#,reflection,system.reflection,C#,Reflection,System.reflection,我正在尝试在asp.NETMVC中使用一些插件。我的插件DLL有一个控制器和一个描述符/模块类 控制器签名如下所示: [Export("SomeController", typeof(System.Web.Mvc.IController))] [PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.NonShared)] public class SomeController : System.Web.Mvc.Co

我正在尝试在asp.NETMVC中使用一些插件。我的插件DLL有一个控制器和一个描述符/模块类

控制器签名如下所示:

[Export("SomeController", typeof(System.Web.Mvc.IController))]
[PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.NonShared)]
public class SomeController : System.Web.Mvc.Controller
{ }
模块描述符类具有以下签名:

[Export("SomeModule", typeof(Plugins.IModule))]
[PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.NonShared)]
public class SomeModule : Plugins.IModule
{ }
现在这个“插件”程序集是动态加载的,没有问题。使用GetTypes()和GetExportedTypes()方法时会出现问题:

因此,问题是,给定上面的类签名,GetTypes()和GetExportedTypes()都只返回SomeController类(不使用Where子句)。任何一种方法都不会列出SomeModule类!在使用Where子句执行下一条语句之前,我已经检查了设置断点并在即时命令窗口上调用GetTypes()和GetExportedTypes()


那为什么呢?为什么我的模块类即使是一个公共类,也具有导出属性,却被忽略了?

您的代码适合我;下面是我在一个空的WinForms应用程序外壳中尝试的代码

如果我不得不猜测你的问题,我相信这与以下任何一个有关:

FirstOrDefault:您查看的程序集是否有可能有两个实现IModule的类,并且找到的第一个类不是SomeModule,而是不同的类?这是代码永远找不到某个模块的一种方式

GetModuleEntry中对IModule的引用:在定义GetModuleEntry的项目中,是否可能引用了包含接口IModule的程序集的更新或不同版本?除非GetModuleEntry方法引用的是包含SomeModule的程序集引用的IModule定义的确切DLL,否则您可能会遇到问题

我认为后者是最有可能的,或者至少会为你指出解决问题的正确方向

以下是运行良好的测试代码,因为只有一个类实现IMyInterface(所以FirstOrDefault是可以的),并且所有类/接口都在同一个模块中:

public interface IMyInterface
{ }

public class bar
{ }

[Export("SomeController", typeof(bar))]
[PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.NonShared)]
public class foo : bar
{ }

[Export("SomeModule", typeof(IMyInterface))]
[PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.NonShared)]
public class ifoo : IMyInterface
{ }

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        Assembly a = System.Reflection.Assembly.GetExecutingAssembly();

        Type type = a.GetTypes()
            .Where(t => t.GetInterface(typeof(IMyInterface).Name) != null).FirstOrDefault();

        if (type != null) // type is of Type ifoo
        {
            Console.WriteLine("Success!"); 
        }

    }
}
public interface IMyInterface
{ }

public class bar
{ }

[Export("SomeController", typeof(bar))]
[PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.NonShared)]
public class foo : bar
{ }

[Export("SomeModule", typeof(IMyInterface))]
[PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.NonShared)]
public class ifoo : IMyInterface
{ }

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        Assembly a = System.Reflection.Assembly.GetExecutingAssembly();

        Type type = a.GetTypes()
            .Where(t => t.GetInterface(typeof(IMyInterface).Name) != null).FirstOrDefault();

        if (type != null) // type is of Type ifoo
        {
            Console.WriteLine("Success!"); 
        }

    }
}