How do I check if class inherits from my class DataSource (abstract class).

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().

This link shows the reverse process - discovering all the derivations of a base class.

Discovering derived types using reflection

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top