Reputation: 21
I have
public class BaseClass {
public string Name;
}
public class A<T> : BaseClass {
public T Value;
public Action<T> ToDo;
}
public List<BaseClass> MyList;
I fill MyList with
A<int>, A<double>, A<SomeUnknownType>.
When processing the elements of MyList, I want to call ToDo(Value) without having to know what the specific T is in each case.
Upvotes: 2
Views: 94
Reputation: 13114
Smells bad, but you can try this:
foreach (var item in MyList)
{
if (item.GetType().IsGenericType)
{
Type genericType = item.GetType().GetGenericArguments()[0];
...
}
}
And you get the generic type into the genericType variable.
In order to determine if the type is A, you can do this:
bool isA = item.GetType().GetGenericTypeDefinition().Equals(typeof(A<>));
Upvotes: 2
Reputation: 11389
you can simply use typeof
comparison between the current enumeration result (a.k.a "item") like if(item.GetType().Equals(typeof(A<>))
Upvotes: -1