0

For a given repository interface IRepository<T> where T: Base and a bunch of classes that inherit Base (for instance, Text) how do you create instances of a provider that implements IRepository, for instance JsonRepository<T> with Text as T (JsonRepository<Text>) and run methods on it?

I'd like to use reflection with a for or foreach to create instances of repositories for all the classes inheriting Base and perform some data management functions on them.

What I want to avoid - if you had 100 classes inheriting Base class you obviously wouldn't go creating instances manually in code and then running it?

2
  • It sounds to me like you have completely the wrong idea here. Data management functions? That doesn't sound like OLTP me to me, which is really the only place that repositories are useful. If you're doing bulk data management or ETL, not so much. Don't let your abstractions pin you down into only one way of looking at a problem.
    – jason
    May 14, 2012 at 17:50
  • I am fuzzy on what you are asking here. Are you saying you already have 100 classes that inherit Base and you want get instances of those classes via reflection so you don't have to hard code the 100 class names? May 14, 2012 at 17:55

3 Answers 3

0

You can use Activator.CreateInstance

See an example here:

Modified to your example:

var d1 = typeof(JsonRepository<>);
Type[] typeArgs = { typeof(Text) };
var makeme = d1.MakeGenericType(typeArgs);
object o = Activator.CreateInstance(makeme);
7
  • Why do you assume the constructor is parameterless?
    – jason
    May 14, 2012 at 17:58
  • Because most data sources have a connection string or a path to file or some indication where the source is.
    – jason
    May 14, 2012 at 18:05
  • this looks promising but still requires hand coding typeof(Text)...preferably I'd like to have a foreach that would enumerate those types that inherit Base
    – mare
    May 14, 2012 at 18:12
  • Yes, you would need to create a loop and just use the types of all Base types you want to create. Let me see if I can find an example
    – SwDevMan81
    May 14, 2012 at 18:16
  • This question could help Getting all types that implement an interface. Then just loop through the types and use the type instead of typeof(Text)
    – SwDevMan81
    May 14, 2012 at 18:17
0

With @SwDevMan81's help and some reflection I managed to hack together this and it actually works perfectly:

var type = typeof(Base);
var types = AppDomain.CurrentDomain.GetAssemblies().ToList()
        .SelectMany(s => s.GetTypes())
        .Where(p => type.IsAssignableFrom(p));

var jsonProviderClass = typeof(JsonContentProvider<>);
var xmlProviderClass = typeof(XmlContentProvider<>);

    foreach (var t in types) {
        Type[] typeArgs = { t };
        // get XML provider
        var xmlGenericProvider = xmlProviderClass.MakeGenericType(typeArgs);
        object xmlProvider = Activator.CreateInstance(xmlGenericProvider);
        // get JSON provider
        var jsonGenericProvider = jsonProviderClass.MakeGenericType(typeArgs);
        object jsonProvider = Activator.CreateInstance(jsonGenericProvider);
        // methods to run
        MethodInfo xmlMethod = xmlGenericProvider.GetMethod("GetContent", new Type[] { typeof(string) });
        MethodInfo jsonMethod = jsonGenericProvider.GetMethod("Update");
        // get content from XML provider
        var result = xmlMethod.Invoke(xmlProvider, new object[] { string.Empty });
        // enumerate XML content items
        foreach (var item in ((IEnumerable)result)) {
            // update them via JSON provider
            object[] parametersArray = new object[] { item };
            var update = jsonMethod.Invoke(jsonProvider, parametersArray);
        }
    }
-1

You currently use a generic constraint but why not use the 'new' constraint which denotes the generic type has a public parameterless ctor i.e.

IRepository<T> where T: Base, new

Which would then mean you could call:

abstract class ctor()
{
    IRepository rep = new T();
}

Now how you apply this may be through a base abstract class or you could simply return the rep instance by means of a propfull... It's upto you.

I have to admit your question is a little fuzzy so please excuse if this is not what you are getting at.

7
  • That wont work, you would only be able to create instances of the Base class.
    – SwDevMan81
    May 14, 2012 at 18:10
  • But Base could be an interface/abstract class May 14, 2012 at 18:11
  • It certainly could, but its not an IRepository inferface like in your example
    – SwDevMan81
    May 14, 2012 at 18:12
  • In your example, T derives from Base and you can create a new instance of it (if it has a parameterless constructor). It says nothing about T implementing IRepository
    – SwDevMan81
    May 14, 2012 at 18:14
  • Hmm yes I see what you mean... Okay my answer is not going to cut the mustard. May 14, 2012 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.