簡體   English   中英

如何獲取解決方案項目中的所有類型?

[英]How to get all types in solution projects?

我有一個解決方案和多個 .net 核心項目。

  • 項目(解決方案)
    • WebAPI
    • 商業
    • 數據

WebApi項目引用Business ,但不引用Data項目。 所以我想在實現IMyInterface的解決方案中獲取所有類型。 所有項目都有實現IMyInterface的類。 我想找到所有這些。

我在 WebApi 項目中調用以下代碼:

        var m = AppDomain.CurrentDomain.GetAssemblies()
            .SelectMany(name => name.GetTypes())
            .Where(type => typeof(IMyInterface).IsAssignableFrom(type))
            .ToList();

但沒有找到類型。

我怎樣才能找到?

你應該得到這樣的所有裝配項目

public Assembly[] GetAssemblies()
{
      var dataAssembly = typeof(AnClassInDataLayer).Assembly;
      var businessAssembly = typeof(ACLassInBusinessLayer).Assembly;
      var webApiAssembly = typeof(Startup).Assembly;
      return new Assembly[] { businessAssembly , dataAssembly, webApiAssembly  };
}

然后

var m = GetAssemblies()
        .SelectMany(name => name.GetTypes())// or .SelectMany(a => a.ExportedTypes)
        .Where(type => typeof(IMyInterface).IsAssignableFrom(type))
        .ToList();

更新

在.Net Core中,如果WebApi項目引用了business層, Business層引用了Data層,那么在WebApi層就可以訪問Data層了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM