1

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 :(

Safiron
  • 883
  • 4
  • 11
  • 30
  • The code you present does not seem to have anything to do with the problem you are asking about. Have you tried using the [`Type.BaseType` property](http://msdn.microsoft.com/en-us/library/vstudio/system.type.basetype)? – O. R. Mapper May 02 '14 at 12:07
  • 2
    You can use [`IsAssignableFrom`](http://msdn.microsoft.com/en-us/library/system.type.isassignablefrom.aspx) to check if a type inherits from another type (or implements an interface) but I'm not sure how you'd do it in LINQ – T. Kiley May 02 '14 at 12:07

2 Answers2

5

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

IsAssignableFrom().

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

Discovering derived types using reflection

Community
  • 1
  • 1
PhillipH
  • 6,182
  • 1
  • 15
  • 25