Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / April 2007

Tip: Looking for answers? Try searching our database.

How to use generics with reflection?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Carlo Folini - 10 Apr 2007 11:10 GMT
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).ClientCredentials.UserName.UserName =
"jimmy";

How can I achieve this?

Signature

Carlo Folini

Günter Prossliner - 10 Apr 2007 12:01 GMT
Hallo Carlo!

> I have a runtime generated class (a WCF proxy) that extends a generic
> type. The generated code is:
[quoted text clipped - 7 lines]
> The problem is that _MyType is also generated code, so I can't cast
> to the correct type:

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

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

GP
Carlo Folini - 10 Apr 2007 13:16 GMT
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
Signature

Carlo Folini

> Hallo Carlo!
>
[quoted text clipped - 18 lines]
>
> GP
Günter Prossliner - 10 Apr 2007 16:29 GMT
Hi Carlo!

> 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
Steven Cheng[MSFT] - 11 Apr 2007 03:41 GMT
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.
Steven Cheng[MSFT] - 13 Apr 2007 02:36 GMT
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.
Carlo Folini - 18 Apr 2007 08:50 GMT
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.

Signature

Carlo Folini

> Hi Carlo,
>
[quoted text clipped - 9 lines]
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
Steven Cheng[MSFT] - 23 Apr 2007 03:14 GMT
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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.