I have several templated objects that all implement the same interface:
I.E.
MyObject<datatype1> obj1;
MyObject<datatype2> obj2;
MyObject<datatype3> obj3;
I want to store these objects in a List… I think I would do that like this:
private List<MyObject<object>> _myList;
I then want to create a function that takes 1 parameter, being a datatype, to see if an object using that datatype exists in my list…. sorta clueless how to go about this. In Pseudo code it would be:
public bool Exist(DataType T)
{
return (does _myList contain a MyObject<T>?);
}
Some Clarification….
My interface is IMyObject<T>, my objects are MyObject<T>. I have a new class MyObjectManager which I need to have a List of MyObject<T> stored within. I need a function to check if a MyObject<T> exists in that list. The type T are datatypes which were auto-generated using T4…. POCO classes from my Entity Data Model.
You can make a generic function:
Note that this requires that you know
Tat compile-time.If all you have is a
System.Typeobject at runtime, you’ll need to use reflection:Note, however, that a
List<MyObject<object>>cannot hold aMyObject<SomeType>.You need to change the list to a
List<object>, or makeMyObjectimplement or inherit a non-generic type and make the list contain that type.