473,397 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Complex Casting problem in Generics

hi

I am having some difficulty in casting using generics. These are the
classes.

public interface IProvider<PROF>
where PROF : IProviderProfile
{
//Some properties/methods
}

public interface IAuthenticationProvider<PROF, TOK, CRED:
IProvider<PROF>
where PROF : IAuthenticationProviderProfile
where TOK : IToken
where CRED : ICredential
{
//Some methods.
}

public class ADAMAuthenticationProvider :
IAuthenticationProvider<ADAMAuthenticationProvider Profile,
ADAMUserToken, UsernamePasswordCredential>
{
}

The first two are interfaces. The third class is the concrete provider.
My problem is that I cannot cast an instance of
ADAMAuthenticationProvider to IProvider<IProviderProfile>.

I should note here that ADAMAuthenticationProviderProfile derives from
IAuthenticationProviderProfile which derives from IProviderProfile.
Similarly the token and credential concrete classes are derived from
the interfaces mentioned in the constraints in the 2nd interface.

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).

Regards,
Ajeet.

Jan 18 '07 #1
7 2178
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<stringto 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<stringsl = new List<string>();
List<objectol = sl;
ol.Add (new object());

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

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?

Jon

Jan 18 '07 #2
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<stringto 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<stringsl = new List<string>();
List<objectol = 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?
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
Jan 18 '07 #3

"Ajeet" <as******@gmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...
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<stringto 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<stringsl = new List<string>();
List<objectol = 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<BASEPROFCovary<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

Jan 18 '07 #4
Hi Ajeet,

a solution could be, to have a non generic Interface IProvider from wich
IProvider<derives.
Then you could cast to this Interface instead of casting to
IProvider<IProviderProfile>

public interface IProvider<PROF: IProvider where PROF : IProviderProfile
{
}

Would this help you?

"Ajeet" <as******@gmail.comschrieb im Newsbeitrag
news:11**********************@s34g2000cwa.googlegr oups.com...
hi

I am having some difficulty in casting using generics. These are the
classes.

public interface IProvider<PROF>
where PROF : IProviderProfile
{
//Some properties/methods
}

public interface IAuthenticationProvider<PROF, TOK, CRED:
IProvider<PROF>
where PROF : IAuthenticationProviderProfile
where TOK : IToken
where CRED : ICredential
{
//Some methods.
}

public class ADAMAuthenticationProvider :
IAuthenticationProvider<ADAMAuthenticationProvider Profile,
ADAMUserToken, UsernamePasswordCredential>
{
}

The first two are interfaces. The third class is the concrete provider.
My problem is that I cannot cast an instance of
ADAMAuthenticationProvider to IProvider<IProviderProfile>.

I should note here that ADAMAuthenticationProviderProfile derives from
IAuthenticationProviderProfile which derives from IProviderProfile.
Similarly the token and credential concrete classes are derived from
the interfaces mentioned in the constraints in the 2nd interface.

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).

Regards,
Ajeet.

Jan 18 '07 #5

Ben Voigt wrote:
"Ajeet" <as******@gmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...
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<stringto 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<stringsl = new List<string>();
List<objectol = 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<BASEPROFCovary<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
}
yeah .. i thought more about the explicit casting approach. There are
other drawbacks. The principal one that I see is that there will be no
runtime polymorphism anymore. Plus, if I later want to cast it back to
the original type, that would prove impossible.

Do you guys have any solution that will address these problems?

I am currently calling the methods through reflection. This makes all
the interfaces pretty much useless. I don't see a good solution to the
problem though.
>
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
Jan 19 '07 #6

Christof Nordiek wrote:
Hi Ajeet,

a solution could be, to have a non generic Interface IProvider from wich
IProvider<derives.
Then you could cast to this Interface instead of casting to
IProvider<IProviderProfile>

public interface IProvider<PROF: IProvider where PROF : IProviderProfile
{
}

Would this help you?
No this would be of no use. I need to cast into that interface because
that interface contains some methods that I need to call. Doing this
would leave me with an empty interface which I will not be able to use.
"Ajeet" <as******@gmail.comschrieb im Newsbeitrag
news:11**********************@s34g2000cwa.googlegr oups.com...
hi

I am having some difficulty in casting using generics. These are the
classes.

public interface IProvider<PROF>
where PROF : IProviderProfile
{
//Some properties/methods
}

public interface IAuthenticationProvider<PROF, TOK, CRED:
IProvider<PROF>
where PROF : IAuthenticationProviderProfile
where TOK : IToken
where CRED : ICredential
{
//Some methods.
}

public class ADAMAuthenticationProvider :
IAuthenticationProvider<ADAMAuthenticationProvider Profile,
ADAMUserToken, UsernamePasswordCredential>
{
}

The first two are interfaces. The third class is the concrete provider.
My problem is that I cannot cast an instance of
ADAMAuthenticationProvider to IProvider<IProviderProfile>.

I should note here that ADAMAuthenticationProviderProfile derives from
IAuthenticationProviderProfile which derives from IProviderProfile.
Similarly the token and credential concrete classes are derived from
the interfaces mentioned in the constraints in the 2nd interface.

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).

Regards,
Ajeet.
Jan 19 '07 #7
"Ajeet" <as******@gmail.comschrieb im Newsbeitrag
news:11**********************@l53g2000cwa.googlegr oups.com...
>
Christof Nordiek wrote:
>Hi Ajeet,

a solution could be, to have a non generic Interface IProvider from wich
IProvider<derives.
Then you could cast to this Interface instead of casting to
IProvider<IProviderProfile>

public interface IProvider<PROF: IProvider where PROF :
IProviderProfile
{
}

Would this help you?

No this would be of no use. I need to cast into that interface because
that interface contains some methods that I need to call. Doing this
would leave me with an empty interface which I will not be able to use.
You can cast to the IProvider interface. Give the IProvider interface all
methods you will need in the general case. The implement them either
implicitly or explicitly by calling the resp. method with typeparameter as
return type.

public interface IProvider
{
IProviderProfile GetProfile();
void OtherMethod();
}

public interface IProvider<PROF>: IProvider where PROF : IProviderProfile
{
PROF GetProfile();
}

public class MyProvider : IProvider<MyProfile>
{
public IProviderProfile GetProfile()
{
implementation goes here.
}

IProvider.GetProfile() { return GetProfile(); }

public OtherMethod()
{
some other impl. here
}
}

will this work for you?
>"Ajeet" <as******@gmail.comschrieb im Newsbeitrag
news:11**********************@s34g2000cwa.googleg roups.com...
hi

I am having some difficulty in casting using generics. These are the
classes.

public interface IProvider<PROF>
where PROF : IProviderProfile
{
//Some properties/methods
}

public interface IAuthenticationProvider<PROF, TOK, CRED:
IProvider<PROF>
where PROF : IAuthenticationProviderProfile
where TOK : IToken
where CRED : ICredential
{
//Some methods.
}

public class ADAMAuthenticationProvider :
IAuthenticationProvider<ADAMAuthenticationProvider Profile,
ADAMUserToken, UsernamePasswordCredential>
{
}

The first two are interfaces. The third class is the concrete provider.
My problem is that I cannot cast an instance of
ADAMAuthenticationProvider to IProvider<IProviderProfile>.

I should note here that ADAMAuthenticationProviderProfile derives from
IAuthenticationProviderProfile which derives from IProviderProfile.
Similarly the token and credential concrete classes are derived from
the interfaces mentioned in the constraints in the 2nd interface.

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).

Regards,
Ajeet.

Jan 19 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: c | last post by:
Hello All, I am struggling with simple casting issue within c# and would really appreciate some insight into where I am going wrong. I have two classes, ClassA and ClassB. Each implements a...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
4
by: KC | last post by:
Could some one explain to me the casting rules for sending generic lists, ex. List<Person>, to a function that accepts List<object>? I cannot get the following easy-cheesy app to work. I get the...
4
by: krahenbuhl | last post by:
Dear All, I've a question related to Generics and casting in c# 2.0. I've a class called Client which implements interface IClient. I'd like to do: LinkedList<Client> clients; public...
5
by: anders.forsgren | last post by:
This is a common problem with generics, but I hope someone has found the best way of solving it. I have these classes: "Fruit" which is a baseclass, and "Apple" which is derived. Further I have...
8
by: Kris Jennings | last post by:
Hi, I am trying to create a new generic class and am having trouble casting a generic type to a specific type. For example, public class MyClass<Twhere T : MyItemClass, new() { public...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
3
by: psyCK0 | last post by:
Hi all! I have a problem of casting generics to their base type. In the code below I first define a BaseList class that can hold items of any type that inherits from BaseItem. I then define a...
3
by: =?Utf-8?B?TWlydHVs?= | last post by:
Hi I'm currently working with vbscripting through MSScriptControl. We have shared some of our objects that should be available for scripting. Some of the functions of these objects will return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.