C# 程序集C中的类列表

C# 程序集C中的类列表,c#,.net,class,.net-assembly,C#,.net,Class,.net Assembly,我想要一个程序集中的类列表,作为输出,我想要一个列表[接口]而不是列表[字符串],因为接口是从中继承程序集中的类的接口。我不知道我的问题是否有道理,但如果有人能给出答案,我将非常感激。 我已经尝试过这个解决方案:,但是它给出了一个包含类名称的列表[string],所以它没有帮助,因为我需要从接口继承的类的列表。 谢谢大家,祝大家今天愉快: 作为对我的问题的编辑,我使用activator.createinstancetype t创建我的类的实例,下面是代码: Assembly assembl

我想要一个程序集中的类列表,作为输出,我想要一个列表[接口]而不是列表[字符串],因为接口是从中继承程序集中的类的接口。我不知道我的问题是否有道理,但如果有人能给出答案,我将非常感激。 我已经尝试过这个解决方案:,但是它给出了一个包含类名称的列表[string],所以它没有帮助,因为我需要从接口继承的类的列表。 谢谢大家,祝大家今天愉快:

作为对我的问题的编辑,我使用activator.createinstancetype t创建我的类的实例,下面是代码:

   Assembly assembly = typeof(Interface1<double, double>).Assembly;

   List<Interface> Classes = new List<Interface>();

   foreach (Type type in assembly.GetExportedTypes())

   {
      var Attributes = type.GetCustomAttributes<FilterAttribute>();
      //select the classes with attribute [Filter]

      if (Attributes.Any())

      {
                TypeOfFilters.Add(type);

      }

      foreach (var i in TypeOfFilters)

      {
            var inst = Activator.CreateInstance(i);

            Classes.Add((Interface) inst);


      }

   }

我收到错误System.IO.FileNotFoundException:无法加载文件或程序集

如果我正确理解您的问题,应该是这样的:

var list = System.Reflection.Assembly.GetTypes().Where(x => typeof(IYourInterface).IsAssignableFrom(x)).ToList();

这是几周前我无聊的时候上的一节小课,如果我理解正确的话,这就是你要问的

您的应用程序必须实现IPlugin,并且必须放在执行目录中的Plugins文件夹中

public interface IPlugin
{
    void Initialize();
}
public class PluginLoader
{
    public List<IPlugin> LoadPlugins()
    {
        List<IPlugin> plugins = new List<IPlugin>();

        IEnumerable<string> files = Directory.EnumerateFiles(Path.Combine(Directory.GetCurrentDirectory(), "Plugins"),
            "*.dll",
            SearchOption.TopDirectoryOnly);

        foreach (var dllFile in files)
        {
            Assembly loaded = Assembly.LoadFile(dllFile);

            IEnumerable<Type> reflectedType =
                loaded.GetExportedTypes().Where(p => p.IsClass && p.GetInterface(nameof(IPlugin)) != null);

            plugins.AddRange(reflectedType.Select(p => (IPlugin) Activator.CreateInstance(p)));
        }

        return plugins;
    }
}
以及DLL文件-还引用System.ComponentModel.Composition命名空间来设置ExportAttribute


但这对我没用。好啊怎么做?这要么是你链接到的问题的副本,除非你指定它是如何不起作用的。或者它是离题的,因为你没有在文章中提供所有相关信息。考虑添加A,这样就清楚了你的问题是什么。你可以用你的问题来添加这些信息。你所说的“不起作用”是什么意思?你有例外吗?预期的结果?你期望的是什么?请展示您在任何地方尝试过的内容。它给了我一个列表,这不是我要找的。@ABIR和您要找的是什么?如果这是ABIR想要的,我将使用MEFManaged Extensibility Framework,而不是通过Activator类自己创建实例。MEF机制是为了在运行时加载和实例化实现指定接口的类而创建的,这是一种更安全的方法。感谢@PaulWeiland,我只使用过Activator和Delegate方法,但我将不得不看看MEF,我正在使用它作为我正在使用的插件框架。先用Activator.CreateInstance破解了一些代码,直到我的一个同事把我的地址告诉了MEF。这让我的生活轻松了很多。对于Op:这里有一个关于MEF的简短教程,这里肯定可以看到它的好处,而不是使用激活器和创建自己的逻辑来从特定目录中获取插件。谢谢你的回答@ColiM,这正是我需要的。但是我得到了错误System.IO.FileNotFoundException:无法加载文件或程序集我认为问题在于目录。GetCurrentDirectory为我提供了一个完全不同的目录C:\Users\AppData\Local\JetBrains\Installations\ReSharperPlatformVs14,而不是包含程序集的目录。
namespace ConsoleApplication18
{
    using System;
    using System.ComponentModel.Composition;
    using System.ComponentModel.Composition.Hosting;
    using System.IO;
    using System.Linq;

    public interface IPlugin
    {
        void Initialize();
    }
    class Program
    {
        private static CompositionContainer _container;
        static void Main(string[] args)
        {
            AggregateCatalog catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new DirectoryCatalog(Path.Combine(Directory.GetCurrentDirectory(), "Plugins")));

            _container = new CompositionContainer(catalog);

            IPlugin plugin = null;
            try
            {
                _container.ComposeParts();

                // GetExports<T> returns an IEnumerable<Lazy<T>>, and so Value must be called when you want an instance of the object type.
                plugin = _container.GetExports<IPlugin>().ToArray()[0].Value;
            }
            catch (CompositionException compositionException)
            {
                Console.WriteLine(compositionException.ToString());
            }

            plugin.Initialize();
            Console.ReadKey();
        }
    }
}
namespace FirstPlugin
{
    using System.ComponentModel.Composition;

    [Export(typeof(IPlugin))]
    public class NamePlugin : IPlugin
    {
        public void Initialize() { }
    }
}