简体   繁体   English

反思-检查家长班

[英]Reflection - check for parent class

How do I check if class inherits from my class DataSource (abstract class). 如何检查类是否从我的类DataSource(抽象类)继承。

here is what I got: 这是我得到的:

var q = from t in Assembly.Load(new AssemblyName("DefaultDataSources")).GetTypes()
                where t.IsClass
                select t;

I don't know what condition to add :( 我不知道要添加什么条件:(

It sounds like you just want: 听起来您只想要:

var query = Assembly.Load(...)
                    .GetTypes()
                    .Where(t => typeof(DataSource).IsAssignableFrom(t));

(The IsAssignableFrom part is the interesting bit, but I gave the full query as this is a good example of a case where a query expression just gets in the way - a single call to the Where extension method is simpler.) IsAssignableFrom部分是有趣的一点,但是我给出了完整的查询,因为这是查询表达式遇到问题的一个很好的例子-对Where扩展方法的单个调用更简单。)

IsAssignableFrom(). IsAssignableFrom()。

This link shows the reverse process - discovering all the derivations of a base class. 该链接显示了相反的过程-发现基类的所有派生。

Discovering derived types using reflection 使用反射发现派生类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM