我想在以下程序集中初始化类,这些类使用反射从EntityBase
类继承。
我猜测lambda表达式是正确的,但我不知道如何从EntityBase
获取这两个类(汇编中有2个继承types2
的类)。
Assembly a = Assembly.LoadFrom("X:\\Workspace\\Operations\\ItemSupplierSetupRequest\\Main\\Source\\ItemSupplierSetupRequest.Entity\\bin\\Debug\\xxxx.ItemSupplierSetupRequest.Entity.dll");
IEnumerable<Type> types2 =
a.GetTypes().Where(x => x.BaseType.ToString().Equals("xxxx.ItemSupplierSetupRequest.Entity.EntityBase"));
我也试过
var result =
a.GetTypes().Where(x => x.BaseType.FullName.Equals("xxxx.ItemSupplierSetupRequest.Entity.EntityBase"));
但不知道如何使用或检查这是否会返回这两个类?
答案 0 :(得分:3)
您的查询应该可行。但是没有必要使用Equals()
或使用字符串比较类型。您可以使用(假设EntityBase
在引用的程序集中,并且其名称空间位于using
)中:
a.GetTypes().Where(x => x.BaseType == typeof(EntityBase))
请注意,这不会返回从EntityBase
继承的所有类型,只返回从直接继承的那些类型。