c# - Compare without generics -
There is a little problem to solve me.
I have a list of items, though later the list. Two of the examples will be filled in different types
The first type is Now I need to run the list of objects and ask which of these items is there then I need to make some custom arguments on the item example for:. The problem can be T or T1 and T2 in any way, so how can I write such a thing - what is a keyword statement that only types the waiver of the forgiveness , But the generic is not inside? Just refer to T1 And need to compare without T2 .. How to solve any idea You can use the property of MyFirstType & LT; T1, T2 & gt; and the other is
MySecondType & LT; T & gt;
foreach (object obj in the list) {if (obj MyFirstType & lt; T1, T2 & gt;) {// do something} and if (obj is MySecondType & lt; ... {}} {}}
If (obj MyFirstType & lt; T1, T2 & gt;) does not work, because it needs solid type for T1 and T2
IsGenericType If you can type
type , then check
obj.GetType () is normal and
GetGenericTypeDefinition is the general type definition, which is To be used to get the typeof (MyFirstType & LT ;, & gt;) and
typeof (MySecondType :
foreach (list obj) {if (obj .GetType () IsGenericType} {if (obj.GetType () GetGenericTypeDefinition () == typeof .. (MyFirstType & lt;; & gt;)) {// do something} else if (obj.GetGenericTypeDefinition () == typeof (MySecondType & lt; & gt;)) {}}}
Comments
Post a Comment