I have a class that has a Generic type "G"
In my class model i have
public class DetailElement : ElementDefinition
Let's say i have a method like this
public void DoSomething<G>(G generic)
where G : ElementDefinition
{
if (generic is DetailElement)
{
((DetailElement)generic).DescEN = "Hello people"; //line 1
//////
ElementDefinition element = generic;
((DetailElement)element).DescEN = "Hello again"; //line 3
//////
(generic as DetailElement).DescEN = "Howdy"; //line 5
}
else
{
//do other stuff
}
}
Compiler reports one error in line 1:
Cannot convert type 'G' to 'DetailElement'
But line 3 works fine. I can workaround this issue by doing the code written in line 5.
What i would like to know is why does the compiler reports the error in line 1 and not the one in line 3, given that, as far as i know, they are identical.
edit: I am afraid i might be missing some important piece of the framework logic
edit2: Although solutions for the compiler error are important, my question is about why the compiler reports an error on line 1 and not in line 3.
If G
was constrained to be a DetailElement
(where G : DetailElement
) then you can go ahead and cast G
to ElementDefinition, i.e., "(ElementDefinition) generic
". But because G
might be another subclass of ElementDefinition
other than DetailElement
at run-time it won't allow it at compile-time where the type is unknown and unverifiable.
In line 3 the type you cast from is known to be an ElementDefinition
so all you're doing is an up-cast. The compiler doesn't know if it will be a succcesful cast at run-time but it will trust you there. The compiler is not so trusting for generics.
The as
operator in line 5 might also return null and the compiler doesn't statically check the type to see if it's safe in that case. You can use as
with any type, not just ones that are compatible with ElementDefinition
.
From Can I Cast to and from Generic Type Parameters? on MSDN:
The compiler will only let you implicitly cast generic type parameters to object, or to constraint-specified types.
Such implicit casting is of course type safe, because any incompatibility is discovered at compile-time.
The compiler will let you explicitly cast generic type parameters to any interface, but not to a class:
interface ISomeInterface {...} class SomeClass {...} class MyClass<T> { void SomeMethod(T t) { ISomeInterface obj1 = (ISomeInterface)t;//Compiles SomeClass obj2 = (SomeClass)t; //Does not compile } }
However, you can force a cast from a generic type parameter to any other type using a temporary object variable
void SomeMethod<T>(T t) { object temp = t; MyOtherClass obj = (MyOtherClass)temp; }
Needless to say, such explicit casting is dangerous because it may throw an exception at run-time if the concrete type used instead of the generic type parameter does not derive from the type you explicitly cast to.
Instead of risking a casting exception, a better approach is to use the
is
oras
operators. Theis
operator returns true if the generic type parameter is of the queried type, andas
will perform a cast if the types are compatible, and will return null otherwise.public void SomeMethod(T t) { if(t is int) {...} string str = t as string; if(str != null) {...} }
Generally, upcasting is a code smell. You can avoid it by method overloading. Try this:
public void DoSomething(DetailElement detailElement)
{
// do DetailElement specific stuff
}
public void DoSomething<G>(G elementDefinition)
where G : ElementDefinition
{
// do generic ElementDefinition stuff
}
You can then take advantage of method overloading by using this code:
DetailElement foo = new DetailElement();
DoSomething(foo); // calls the non-generic method
DoSomething((ElementDefinition) foo); // calls the generic method
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