Re: Complex Casting problem in Generics

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"Ajeet" <asgrewal@xxxxxxxxx> wrote in message
news:1169119502.422928.148160@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Jon Skeet [C# MVP] wrote:
Ajeet wrote:
I am having some difficulty in casting using generics. These are the
classes.

<snip>

What do I have to do to be able to cast it as desired? If you can give
me links that describe the solution, I will be grateful (I tried some
googling but to no luck so far).

You can't. As far as I can tell, your problem is the same one as often
bites people that you can't cast from List<string> to List<object>.
That's an easier example to work with, so I'll do so, if you don't mind
:)

Suppose C# generics supported covariance in types. I could do:
List<string> sl = new List<string>();
List<object> ol = sl;
ol.Add (new object());

At that point I've clearly broken the idea that the list should only
contain strings.


that makes sense. I did not think of that.

Can I overload the cast operator and do this?

Should be possible, although it may be better to make an explicit conversion
function.

Something like:

public interface IProvider<PROF>
where PROF : IProviderProfile
{
private readonly PROF internalProfile;
private IProfile(PROF prov) : internalProvider(prov) {}
//Public constructors
//Some properties/methods
public IProvider<BASEPROF> Covary<BASEPROF>() where PROF : BASEPROF
{ return new IProvider<BASEPROF>(internalProfile); } // because
internalProfile casts to BASEPROF

// if IProfile has any internal state, you need to find
a way to share that too,

// possibly via pointer-to-implementation paradigm

// now you also need a generic operator==, not sure if that is possible
}



Now, it could be that your interfaces wouldn't actually have that
problem (i.e. there wouldn't be any methods/properties exposed which
gave rise to the issue), but that's why it's disallowed.

Are you absolutely sure you need generics here in the first place?


We deliberated quite a bit before deciding on generics, so there are
quite a few reasons for doing this. Although I admit we did not foresee
this problem occurring. Do you think there are disadvantages to using
generics?

Jon



.



Relevant Pages

  • Re: Complex Casting problem in Generics
    ... Can I overload the cast operator and do this? ... public interface IProvider ... internalProfile casts to BASEPROF ... Are you absolutely sure you need generics here in the first place? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Can this callback mechanism be generified?
    ... With luck, we'll get reifiable types in Java 7 and then nonsense like that will be unneeded.) ... I did some reading (Java Generics and Collections) and it turns out there are ways to get to the types, ...
    (comp.lang.java.help)
  • Re: configurations and generics
    ... Andy Luotto wrote: ... different generics, something like ... I know this isn't any help, but good luck with that! ... getting configurations to do anything *useful* for ...
    (comp.lang.vhdl)
  • Re: Complex Casting problem in Generics
    ... me links that describe the solution, I will be grateful (I tried some ... googling but to no luck so far). ... Are you absolutely sure you need generics here in the first place? ...
    (microsoft.public.dotnet.languages.csharp)
  • Crossbow weed killer -- Generic Sub yet?
    ... Any sub for Dow's Crossbow -- 2,4-D, butoxyethanol ester and ... I had bad luck when generics for Roundup came out a few years ago, ... Roundup worked fine. ...
    (misc.rural)