2017-07-09 16 views
3

特定の抽象クラスを実装するすべてのクラスを取得しようとしています。私は次のコードでそれをやろうとしています:特定の抽象クラスを実装するすべてのクラスを取得します。

var type = typeof(BaseViewComponent); 
var types = Assembly 
    .GetEntryAssembly() 
    .GetReferencedAssemblies() 
    .Select(Assembly.Load) 
    .SelectMany(s => s.GetTypes()) 
    .Where(p => type.IsAssignableFrom(p)); 

これまでのところ、私は抽象クラスをそれ自身で得ることができます。その基本クラスを実装するクラスはありません。

この抽象基本クラスを実装するすべてのクラスを取得するには、何を変更する必要がありますか?

+0

は、同じアセンブリ内の実装および抽象クラスはありますか? – Nkosi

+0

@ Nkosiいいえ、実装は別のアセンブリにあります。 – Vivendi

答えて

4
using System.Reflection; 
using Microsoft.Extensions.DependencyModel; 
var asmNames = DependencyContext.Default.GetDefaultAssemblyNames(); 
var type = typeof(BaseViewComponent); 

var allTypes = asmNames.Select(Assembly.Load) 
    .SelectMany(t => t.GetTypes()) 
    .Where(p => p.GetTypeInfo().IsSubclassOf(type)); 
関連する問題