Re: Complex Casting problem in Generics



"Ajeet" <asgrewal@xxxxxxxxx> schrieb im Newsbeitrag
news:1169221341.804965.164080@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

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" <asgrewal@xxxxxxxxx> schrieb im Newsbeitrag
news:1169107212.116176.113350@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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<ADAMAuthenticationProviderProfile,
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.




.



Relevant Pages

  • Re: ASP.NET and COM+
    ... You should create a new assembly, which contains the IProvider interface. ... the call to the Serviced Component is remoted, ... This component is registered under COM+ 1.5 as a Server application, it has an strong name, fixed assembly version, fixed applicationid, fixed progid and is installed on the GAC. ...
    (microsoft.public.dotnet.framework.component_services)
  • Re: Complex Casting problem in Generics
    ... IProvider<> derives. ... Then you could cast to this Interface instead of casting to ... I should note here that ADAMAuthenticationProviderProfile derives from ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Complex Casting problem in Generics
    ... IProvider<> derives. ... Then you could cast to this Interface instead of casting to ... I should note here that ADAMAuthenticationProviderProfile derives from ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ASP.NET and COM+
    ... Will this new assembly only have the Interface signatures? ... > Hi Rinaldo, ... > You should create a new assembly, which contains the IProvider interface. ... > the call to the Serviced Component is remoted, ...
    (microsoft.public.dotnet.framework.component_services)
  • Re: Remoting Objects: style question
    ... Most likely, if you use abstract objects, the first cast will be done in the ... class to which it is cast, regardless of whether it derives from that class. ... acobj will be non-null even though the remoted object is ... > the object actually implements the interface). ...
    (microsoft.public.dotnet.framework.remoting)

Loading