C# Casting Generic to Generic where B : A?

This is type contravariance in generics Even though B is a subtype of A Generic) this method can then convert Generic).

This is type contravariance in generics. Even though B is a subtype of A, Generic is not a subtype of Generic, so you can't cast Generic to Generic. Check: msdn.microsoft.com/en-us/library/dd79951... for more details.

You can overload DoSomething() to DoSomething(Generic), this method can then convert Generic to Generic and call DoSomething(Generic).

Well basically converting Generic to Generic would mean, Generic() but that won't help, will it, since B was cast to A but is still also B. Anyway, great answer, you're absolutely right, gonna be hard though to work around this. – Roy T.

Jul 15 at 12:32 He can also try changeing the MyGeneric to the interface type like IEnumerable. Will this work? – oleksii Jul 15 at 12:32 @Roy T you could try an implicit operator msdn.microsoft.Com/en-us/library/z5z9kes2.

Aspx – StuperUser Jul 15 at 12:37 1 @StuperUser, I'll take a look at the implicit operator. Also important to note is that in VS2010 you have the generic contravariance modifier in: msdn.microsoft.Com/en-us/library/dd469484(VS.100). Aspx that allows contravariance, but unfortunately I'm still stuck at 2008/.

NET 3.5 – Roy T. Jul 15 at 12:40 1 @oleksii: That works because IEnumerable is covariant in T (see my answer below). Sadly that is only available in .

NET 4.0.In . NET 3.5 that code will not compile. – InBetween Jul 15 at 14:07.

If your generic class MyGeneric can implement an interface which is covariant in T then you could do the following: Define an interface covariant in T: public interface IMyGeneric { T Foo(); } This means that the generic type T can only be an output type (can not be part of a method signature defined in the interface. ) Make your class MyGeneric implement IMyGeneric. Now through the interface, which is covariant in T, the following code is valid: IMyGeneric classBGeneric = new MyGeneric(); IMyGeneric classAGeneric = classBGeneric; So in your case, you could write the following: public void MyEventHandler(object source, EventArgs e) { DoSomething((IMyGeneric)source); //IMyGeneric is implicitly convertible to IMyGeneric } EDIT: Just read in comments that you are working with .

NET 3.5, so sadly this won't work in your case. I'll leave the answer in case it is of any use to somebody else. Sorry, I missed that important piece of information.

Still this is a good answer, it would solve my issue in 2010, and I forgot to mention in the actual question that I'm stuck at 2008/.NET.3.5 :) – Roy T. Jul 15 at 13:48.

Most Some generic classes have a cast function which does it for you. If it doesn't exist you could try to create your own.

I am not aware that "most generic classes have a Cast function. " I am aware there is a Cast extension method defined for sequences that attempts to cast each item in a sequence. Your answer seems to imply it's part of the generic pattern almost by default.

– Anthony Pegram Jul 15 at 12:32.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions