【问题标题】:Asp.Net Core v1.1 Get Inherited Class Names From another project in same solutionAsp.Net Core v1.1 从同一解决方案中的另一个项目获取继承的类名
【发布时间】:2017-07-07 12:10:13
【问题描述】:

我正在使用不同的继承类,我希望从同一接口继承此类名称

public class CollectorA : ICollector
{
    public string CollectSomething()
    {
        //DO Something
    }
}
public class CollectorB : ICollector
{
    public string CollectSomething()
    {
        //DO Something
    }
}

我想做这样的事情:

public void Init(IServiceCollection serviceCollection)
{

    var types  = AppDomain.CurrentDomain.GetAssemblies()
                          .SelectMany(assembly => assembly.GetTypes())
                          .Where(type => type.IsInstanceOfType(typeof(ICollector))); 

获取类型返回 null 我试过 AppContext 但还是不行。

foreach(var item in types)
{
    serviceCollection.AddTransient<ICollector,item.Name>();
} 

【问题讨论】:

    标签: c# asp.net inheritance asp.net-core


    【解决方案1】:

    .NET Core 中没有 AppDomain 所以这个 AppDomain.CurrentDomain.GetAssemblies() 也不存在(没有 .NET Standard 1.x 有它。)这是 GitHub issue 详细信息 这个。

    如果您想在blog 中使用 PolyFill (AppDomain) 的方法,这里是代码

    public class AppDomain
     {
        public static AppDomain CurrentDomain { get; private set; }
    
        static AppDomain()
        {
            CurrentDomain = new AppDomain();
        }
    
        public Assembly[] GetAssemblies()
        {
            var assemblies = new List<Assembly>();
            var dependencies = DependencyContext.Default.RuntimeLibraries;
            foreach (var library in dependencies)
            {
                if (IsCandidateCompilationLibrary(library))
                {
                    var assembly = Assembly.Load(new AssemblyName(library.Name));
                    assemblies.Add(assembly);
                }
            }
            return assemblies.ToArray();
        }
    
        private static bool IsCandidateCompilationLibrary(RuntimeLibrary compilationLibrary)
        {
            return compilationLibrary.Name == ("Specify")
                || compilationLibrary.Dependencies.Any(d => d.Name.StartsWith("Specify"));
        }
    }
    

    然后你可以这样使用

    var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(ass => ass.ExportedTypes)
                              .Where(type => type.IsInstanceOfType(typeof(ICollector)));
                foreach (var item in types)
                {
                    services.AddTransient(typeof(ICollector), item);
                }
    

    有关替代方法,请参阅此SO post 以获取 GetAssemblies() 或 Types 的替代方法。

    【讨论】:

      【解决方案2】:

      你也可以这样添加

      foreach(var item in types)
      {
          serviceCollection.AddTransient(typeof(ICollector), item);
      } 
      

      在寻找类型时,请尝试以下操作。

      //get collectors
      var types = typeof(CollectorA).Assembly
          .GetTypes()
          .Where(t => typeof(ICollector).IsAssignableFrom(t));
      
      //add to service
      foreach(var item in types)
      {
          serviceCollection.AddTransient(typeof(ICollector), item);
      } 
      

      【讨论】:

      • types 值为 null :)
      • @Batuhan,好的,等等,让我看看。这是什么类型的项目。
      • 类库(ASP.NET-Core)-我正在从控制台应用程序执行此过程。
      • Assembly 不包含 GetAssembly 的定义。哪个库。一定要加吗?
      猜你喜欢
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2019-12-03
      • 2018-05-18
      • 2021-04-19
      • 2019-09-06
      • 2022-11-30
      • 2017-07-19
      相关资源
      最近更新 更多