Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using reflection to get all classes of certain base type in dll

Tags:

c#

reflection

I have a dll that contains a number of classes that all inherit from a CommandBase class. I'm trying to get instances of all of these classes (CommandA, CommandB, CommandC, etc...) using reflection in C# so that I can call a specific method on each one. Here is what I have so far:

//get assemblies in directory.
string folder = Path.Combine(HttpContext.Current.Server.MapPath("~/"), "bin");
var files = Directory.GetFiles(folder, "*.dll");
//load each assembly.
foreach (string file in files)
{
  var assembly = Assembly.LoadFile(file);
  if (assembly.FullName == "MyCommandProject")
  {
    foreach (var type in assembly.GetTypes())
    {
      if (!type.IsClass || type.IsNotPublic) continue;
      if(type is CommandBase)
      {
        var command = Activator.CreateInstance(type) as CommandBase;
      }
    }
  }
}

I'm having 2 issues. The 1st issue is that the line "if(type is CommandBase") gives the following warning:

The given expression is never of the provided type CommandBase.

The 2nd issue is that I can't figure out how to get an instance of the actual object (CommandA, CommandB, etc...), just converting it to CommandBase isn't enough.

like image 701
Justin Avatar asked Jul 28 '10 14:07

Justin


People also ask

Is it good to use reflection in C#?

Some of the situations when reflections are useful in C# are given as follows: Reflections are quite useful for creating new types at runtime. It is easy to use reflection with the program metadata attributes. Reflection is needed to examine and instantiate types in an assembly.

Where do we use reflection in C#?

You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, reflection enables you to access them.

What does reflection mean C#?

Reflection is the process of describing the metadata of types, methods and fields in a code. The namespace System.Reflection enables you to obtain data about the loaded assemblies, the elements within them like classes, methods and value types.

Which class has significant importance for reflection in C#?

Type class is the main class for the . NET Reflection functionality to access metadata. The System. Type class is an abstract class and represents a type in the Common Type System (CLS).


2 Answers

This is the method I use to load up based on an interface.

private static List<T> GetInstances<T>()
{
        return (from t in Assembly.GetExecutingAssembly().GetTypes()
                where t.GetInterfaces().Contains(typeof (T)) && t.GetConstructor(Type.EmptyTypes) != null
                select (T) Activator.CreateInstance(t)).ToList();
}

And here's the same function that pulls back based on base class.

private static IList<T> GetInstances<T>()
{
        return (from t in Assembly.GetExecutingAssembly().GetTypes()
                       where t.BaseType == (typeof(T)) && t.GetConstructor(Type.EmptyTypes) != null
                       select (T)Activator.CreateInstance(t)).ToList();
}

Of course it would need to be modified slightly to point at the reference you're loading.

like image 150
Jeff Sheldon Avatar answered Sep 28 '22 10:09

Jeff Sheldon


You must change

if(type is CommandBase) 

to

if(type.IsSubclassOf(typeof(CommandBase)))

If the IsSubclassOf is the converse of IsAssignableFrom. That is, if t1.IsSubclassOf(t2) is true, then t2.IsAssignableFrom(t1) is also true.

like image 40
Akash Kava Avatar answered Sep 28 '22 11:09

Akash Kava