How to Load an Assembly of Generic Type <T> Dynamically ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • filhodapuc
    New Member
    • Sep 2008
    • 2

    How to Load an Assembly of Generic Type <T> Dynamically ?


    How to Load the class "MyContent" dynamically ?
    I have 1 interface<T>, 1 abstract generic class<T> and 1 class. Check my code out:

    Code:
    public interface IMyObjectInterface{
    }
    public abstract MyAbstractObject : IMyObjectInterface{
    }
    public class MyObject : MyAbstractObject{
    }
    
    public interface IMyContentInterface<T>  where T : MyAbstractObject
    {
        void MyMethod();
    }
    public abstract MyAbstractContent<T>, IMyContentInterface<T>  where T : MyAbstractObject
    {
        public abstract void MyMethod();
    }
    public public class MyContent : MyAbstractContent<MyObject>
    {
        public override void MyMethod() { //do something }
    }
    I am trying but obviously it's not working:
    Code:
    IMyObjectInterface obj = (IMyObjectInterface)Assembly.Load("MyAssembly").CreateInstance("MyObject");
    IMyContentInterface<obj> content = (IMyContentInterface<obj>)Assembly.Load("MyAssembly").CreateInstance("MyContent");
    content.MyMethod();
    //assembly and type names are correct
    If I change IMyContentInter face<obj> to IMyContentInter face<MyObject>, works :
    Code:
    IMyContentInterface<MyObject> content = (IMyContentInterface<MyObject>)Assembly.Load("MyAssembly").CreateInstance("MyContent");
    content.MyMethod();
    //assembly and type names are correct

    The problem is that i don't what is going to be my object in the 2nd line, when defining IMyContentInter face<T>. Please, does somebody know how to do it in .NET Framework 4.0?
  • filhodapuc
    New Member
    • Sep 2008
    • 2

    #2
    Please refer to: http://stackoverflow.com/questions/2...074011#2074011

    Comment

    Working...