How do I Load the class "MyContent" dynamically ?
I have 1 interface<T>
, 1 abstract generic class<T>
and 1 class. Check my code out:
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:
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 IMyContentInterface<obj>
to IMyContentInterface<MyObject>
, works :
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 IMyContentInterface<T>
. Please, does somebody know how to do it in .NET Framework 4.0?
T is called type parameter, which can be used as a type of fields, properties, method parameters, return types, and delegates in the DataStore class. For example, Data is generic property because we have used a type parameter T as its type instead of the specific data type. Note.
The C# var keyword is used to declare implicit type variables in C#.
var can only be used when a local variable is declared and initialized in the same statement; the variable cannot be initialized to null, or to a method group or an anonymous function. var cannot be used on fields at class scope.
the item in the < >
has to be a type not an object.
my car is an object of the type car so
Car myCar=new Car();
i want a list to keep my cars (objects of type Car) in.
List<Car> myCars = new List<Car>();
And then we add object of type Car to my List.
myCars.Add(myCar);
myCars.Add(anotherCar);
How do I Load the class "MyContent" dynamically?
Loading it isn't hard - you already know how to do that, but C# generics are strongly-typed, checked and guaranteed at compile time. Consider this code:
List<string> list = new List<string>();
list.Add(new TcpSocket()); // This line won't compile
The C# compiler couldn't tell you this was illegal if you were allowed to declare generics like this:
Type type = GetTypeFromReflectedAssembly();
List<type> list = new List<type>();
// This *might* work - who knows?
list.Add(new TcpSocket());
If your ultimate goal is to call MyContent.MyMethod()
and that doesn't have anything to do with the generic type parameter <T>
, consider declaring a non-generic interface you can implement somewhere in your inheritance hierarchy and declare your instance variable using that:
IMyContentInterface content = (IMyContentInterface)Assembly.Load("MyAssembly").CreateInstance("MyContent");
content.MyMethod();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With