开发者

Cannot get selected type from GetTypes

I want to initialize classes in the following assembly wh开发者_StackOverflow中文版ich are inheriting from the EntityBase class using reflection.

I am guessing the lambda expression is correct but I don't know how to get those 2 classes (there are 2 classes in assembly which inherit EntityBase) from 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"));

I also tried

var result =
    a.GetTypes().Where(x => x.BaseType.FullName.Equals("xxxx.ItemSupplierSetupRequest.Entity.EntityBase"));

but don't know how to use or to check if this returns those 2 classes?


Your queries should probably work. But there is no need to use Equals() or to compare the types using strings. You can use (assuming EntityBase is in a referenced assembly and its namespace is in a using):

a.GetTypes().Where(x => x.BaseType == typeof(EntityBase))

Keep in mind this will not return all types that inherit from EntityBase, only those that inherit from it directly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜