HighTechTalks DotNet Forums  

How to use generics with reflection?

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss How to use generics with reflection? in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Carlo Folini
 
Posts: n/a

Default How to use generics with reflection? - 04-10-2007 , 06:10 AM






Hi,
I have a runtime generated class (a WCF proxy) that extends a generic type.
The generated code is:

[DebuggerStepThrough, GeneratedCode("System.ServiceModel", "3.0.0.0")]
public class _MyTypeClient : ClientBase<_MyType>, _MyType
{
.....
}
Now I would like to call some methods from an instance of this class.
The problem is that _MyType is also generated code, so I can't cast to the
correct type:
((ClientBase<_MyType>)myInstance).ClientCredential s.UserName.UserName =
"jimmy";

How can I achieve this?


--
Carlo Folini

Reply With Quote
  #2  
Old   
Günter Prossliner
 
Posts: n/a

Default Re: How to use generics with reflection? - 04-10-2007 , 07:01 AM






Hallo Carlo!

Quote:
I have a runtime generated class (a WCF proxy) that extends a generic
type. The generated code is:

[DebuggerStepThrough, GeneratedCode("System.ServiceModel", "3.0.0.0")]
public class _MyTypeClient : ClientBase<_MyType>, _MyType
{
....
}
Now I would like to call some methods from an instance of this class.
The problem is that _MyType is also generated code, so I can't cast
to the correct type:

((ClientBase<_MyType>)myInstance).ClientCredential s.UserName.UserName
= "jimmy";
Why not just:
myInstance.ClientCreditials.UserName.UserName = "jimmy";
?


GP




Reply With Quote
  #3  
Old   
Carlo Folini
 
Posts: n/a

Default Re: How to use generics with reflection? - 04-10-2007 , 08:16 AM



Hi Günter,
the proxy type is dinamically created, so the variable is of type object.
To access the method on it I have to cast it to the appropriate type.

Ciao
--
Carlo Folini


"Günter Prossliner" wrote:

Quote:
Hallo Carlo!

I have a runtime generated class (a WCF proxy) that extends a generic
type. The generated code is:

[DebuggerStepThrough, GeneratedCode("System.ServiceModel", "3.0.0.0")]
public class _MyTypeClient : ClientBase<_MyType>, _MyType
{
....
}
Now I would like to call some methods from an instance of this class.
The problem is that _MyType is also generated code, so I can't cast
to the correct type:

((ClientBase<_MyType>)myInstance).ClientCredential s.UserName.UserName
= "jimmy";

Why not just:
myInstance.ClientCreditials.UserName.UserName = "jimmy";
?


GP




Reply With Quote
  #4  
Old   
Günter Prossliner
 
Posts: n/a

Default Re: How to use generics with reflection? - 04-10-2007 , 11:29 AM



Hi Carlo!

Quote:
the proxy type is dinamically created, so the variable is of type
object. To access the method on it I have to cast it to the
appropriate type.
(_MyTypeClient)myInstance.ClientCredentials... is not possible?


If no: When static typing is not possible, you need to get the System.Type
Objects. Without them it is not possible.

e.g.
Type tClientBase = typeof(ClientBase<>);
Type tType = Type.GetType("Namespace._MyType, Assembly", true);
Type tMyClient = tClientBase.MakeGenericType(tType);

// now you can reflect on tMyClient to invoke Methods late-bound (e.g.
GetProperty on "ClientCredentials").

When you have an underlying and non-generic interface you can use this for
early-bound access.

BTW: Maybe there is a WCF specific solution for you problem.


G




Reply With Quote
  #5  
Old   
Steven Cheng[MSFT]
 
Posts: n/a

Default Re: How to use generics with reflection? - 04-10-2007 , 10:41 PM



Thanks for G?ter's informative input.

Hi Carlo,

Do you mean the "_MyType" type is dynamically compiled and
generated(through .NET compiler api)? If so, I'm afraid you can not
statically use type name in your application code, but have to use
Type.GetType to get the type instance of that dynamic generated
type(through full typename).

G?ter has posted some code snippt on this, after you get the type object of
your dynamic generated proxy/interface, you can use standard reflection API
to invoke method/property against the actual proxy instance.

I think this is a typical dynamic code generation and late-binding case and
WCF hasn't particular solution for it except using reflection.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.


Reply With Quote
  #6  
Old   
Steven Cheng[MSFT]
 
Posts: n/a

Default Re: How to use generics with reflection? - 04-12-2007 , 09:36 PM



Hi Carlo,

Have you got any further ideas on this issue? If you have any further
questions, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.


Reply With Quote
  #7  
Old   
Carlo Folini
 
Posts: n/a

Default Re: How to use generics with reflection? - 04-18-2007 , 03:50 AM



Hi Steve,
I want to use windows authentication, I'm having trouble also with the
'normal' generated proxy.
When I solved those problem I will try Gunter and yours suggestions.
Thanks.

--
Carlo Folini


"Steven Cheng[MSFT]" wrote:

Quote:
Hi Carlo,

Have you got any further ideas on this issue? If you have any further
questions, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.



Reply With Quote
  #8  
Old   
Steven Cheng[MSFT]
 
Posts: n/a

Default Re: How to use generics with reflection? - 04-22-2007 , 10:14 PM



Thanks for your followup Carlo,

Sure, please feel free to post here if you get any progress.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.