I have the following generic class
public class Home<T> where T : Class
{
public string GetClassType
{
get{ return T.ToString() }
}
}
Now, I’m getting an Object X which I know for sure is Home:
public void DoSomething(object x)
{
if(x is // Check if Home<>)
{
// I want to invoke GetClassType method of x
// but I don't know his generic type
x as Home<?> // What should I use here?
}
}
Can I even make a cast without specifying the generic type of the class?
If you’re sure the argument to
DoSomethingwill be aHome<T>, why not make it a generic method?Of course, it would be even easier if
DoSomethingshould logically be an instance method onHome<T>.If you really want to stick with what you have, you could use reflection (untested):