Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I declare a variable of Type<T> without specifying T at compile time?

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?

like image 316
Fabio Costa Avatar asked Jan 14 '10 21:01

Fabio Costa


People also ask

What does the T mean in C#?

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.

Which of the following keyword is used to declare a variable whose type will be automatically determined by the compiler C#?

The C# var keyword is used to declare implicit type variables in C#.

When to use var type 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.


2 Answers

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);
like image 118
gingerbreadboy Avatar answered Sep 27 '22 18:09

gingerbreadboy


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();
like image 39
Jeff Sternal Avatar answered Sep 27 '22 18:09

Jeff Sternal