0

I want to create instances of all classes which are sub classes of a generic base class. I have searched for some time now. Didn't find anything.

public class BaseClass<T> { }
public class CustomSubClass: BaseClass<int> { }

I have problems with the generic type. Anyone who can help me?

redlaz
  • 121
  • 3
  • 9
  • 1
    What have you tried so far? Did you try to iterate over all types and create instances with: object instance = Activator.CreateInstance(type); ? – kassi Jul 14 '16 at 06:32
  • Are all the sub classes located in the same assembly? – Itay Podhajcer Jul 14 '16 at 06:39
  • All sub classes are located in the same assembly. I have tried to CreateInstance but I get a message saying that it cant create instance because of the generic T. – redlaz Jul 14 '16 at 06:44

3 Answers3

3

Suppose you have:

class BaseClass<T> { }

class CustomSubClass : BaseClass<int> { }

class CustomSubClass2 : BaseClass<string> { }

Then :

List<Type> allSubTypes = new List<Type>();
foreach(var assem in AppDomain.CurrentDomain.GetAssemblies())
{
    var subTypes = assem.GetTypes().Where(x => x.BaseType != null 
                         && x.BaseType.IsGenericType 
                         && x.BaseType.GetGenericTypeDefinition() == typeof(BaseClass<>));

    allSubTypes.AddRange(subTypes);
}

// CustomSubClass and CustomSubClass2
foreach (var type in allSubTypes)
{
    object instance = Activator.CreateInstance(type);
}
Zein Makki
  • 29,485
  • 6
  • 52
  • 63
  • Additional information: Cannot create an instance of MYCLASS because Type.ContainsGenericParameters is true. – redlaz Jul 14 '16 at 06:46
  • Can you show your constructor if you have one. Edit your question and add it. It is working from my side. – Zein Makki Jul 14 '16 at 06:49
  • Ah it failed for a specific class which is a bit different. It works like a charm! :-) Thank you! – redlaz Jul 14 '16 at 06:53
  • @redlaz if you want to create a class that has a generic type argument. Then you have to use `type.MakeGenericType(typeof(int));` for example. – Zein Makki Jul 14 '16 at 06:54
  • Yup. It was a class which had a generic type. Than you again! – redlaz Jul 14 '16 at 06:55
0

Sounds like you are implementing a plugin system of some kind. You might want to check out MEF which does this kind of thing for you;

"The Managed Extensibility Framework or MEF is a library for creating lightweight, extensible applications. It allows application developers to discover and use extensions with no configuration required. It also lets extension developers easily encapsulate code and avoid fragile hard dependencies. MEF not only allows extensions to be reused within applications, but across applications as well."

Steve Cooper
  • 20,542
  • 15
  • 71
  • 88
0

having the same question, I end up here. But also looking this answer too.

So I end up with this draft of code merging both part :

// Get all derivative class of BaseClass<T>
    var allSubTypes = AppDomain.CurrentDomain.GetAssemblies()
        .SelectMany(a=>a.GetTypes())
        .Where(x => x.BaseType != null
                             && !x.IsAbstract
                             && (x.GetConstructor(Type.EmptyTypes) != null)
                             && x.BaseType.IsGenericType
                             && x.BaseType.GetGenericTypeDefinition() == typeof(BaseClass<>));

    // and instantiate them
    foreach (var type in allSubTypes)
    {
        object instance = Activator.CreateInstance(type);
    }
TRex
  • 127
  • 1
  • 8