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?
The Java compiler won't let you cast a generic type across its type parameters because the target type, in general, is neither a subtype nor a supertype.
Yes, you can define a generic method in a non-generic class in Java.
Yes, in this case, inheritance is a better solution than composition as you have it, because a StackInteger is a Stack .
Whenever you want to restrict the type parameter to subtypes of a particular class you can use the bounded type parameter. If you just specify a type (class) as bounded parameter, only sub types of that particular class are accepted by the current generic class.
If you're sure the argument to DoSomething
will be a Home<T>
, why not make it a generic method?
public void DoSomething<T>(Home<T> home)
{
...
}
Of course, it would be even easier if DoSomething
should logically be an instance method on Home<T>
.
If you really want to stick with what you have, you could use reflection (untested):
public void DoSomething(object x)
{
// null checks here.
Type t = x.GetType();
if (t.IsGenericType &&
&& t.GetGenericTypeDefinition() == typeof(Home<>))
{
string result = (string) t.GetProperty("GetClassType")
.GetValue(x, null);
Console.WriteLine(result);
}
else
{
... // do nothing / throw etc.
}
}
What if Home derived from a base class?
public class Home
{
public virtual string GetClassType { get; }
}
public class Home<T> : Home
where T : class
{
public override string GetClassType
{
get{ return T.ToString() }
}
...
}
and then
public void DoSomething(object x)
{
if(x is Home)
{
string ct = ((Home)x).GetClassType;
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With