Determining a generic type at runtime
-
I have a class
public class myclass : BindingList<T> {}
I have created(shadowed) the GetEnumerator method to provide some custom sorting and what not. But the problem is the type can be Category, Topic, or Entry. And I need to test each one because each has its own sorting strategy. I triedif( typeof(T) is Category)
but that doesn't work. What is the easiest way to determine the type T is?-------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog
-
I have a class
public class myclass : BindingList<T> {}
I have created(shadowed) the GetEnumerator method to provide some custom sorting and what not. But the problem is the type can be Category, Topic, or Entry. And I need to test each one because each has its own sorting strategy. I triedif( typeof(T) is Category)
but that doesn't work. What is the easiest way to determine the type T is?-------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog
typeof(T)
will always return a type of T, and T is really a type identifier. What you may try is this:public void Sort(T item)
{
if ( item is Category )...
if ( item is Volume ) ...
} -
typeof(T)
will always return a type of T, and T is really a type identifier. What you may try is this:public void Sort(T item)
{
if ( item is Category )...
if ( item is Volume ) ...
}lol. its a GetEnumerator(). Theres no definition like that.
Type t = typeof(T); if( t.Equals(Category) )
I figured it out just a second ago Thanks tho-------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog