Skip to main content
GameDev.net gamedev.net
🔒 Locked

[C#] Generic Functions (a different question)

Started by Verg Feb 19, 2009 at 2:00 PM 4 replies 1.3k views
Original Post
Verg
Verg
It seems C# doesn't handle generics like C++ handles templates. I need a way to do this:

public void DoSomething<T>(object o, T thing)
{
     _member = thing.Value;
}
The error is 'T' does not contain a definition for 'Value'... well, of course it doesn't :) That's what a template type is. Anything I would call "DoSomething" for would have "Value" defined. But apparently not what a generic type is :/ Is there a way to accomplish this in C#--and if so, what is it? Thanks and Greetings Chad
Mike.Popoloski
Mike.Popoloski
You can constrain your generic types to specific interfaces, so you could define an interface for types that expose Value and then call it on that.

C# 4.0 promises to introduce a dynamic type, which will support the concept of duck typing, allowing binding to be done at runtime.
Mike Popoloski | Journal | SlimDX
Verg
Verg
Hi Mike,


Thanks for your reply. Currently I can't refactor the types to expose an interface, as the code was written by another person in the company and generally under their watch.

I suppose I could write an adapter class that handled the types, but then that would completely alleviate the need for a template class at all... and it would mean that every new class coming along would have to be added to the adapter.

I guess it's wait for 4.0 and hope the code doesn't get out of hand :-)


Thanks,


Chad
Mike.Popoloski
Mike.Popoloski
You could always use reflection to retrieve the value. If this isn't going to be used in time-critical code, you shouldn't have to worry much about the implementation. There are tricks to speed up reflection for things that are called many times per frame, but it's more work if you need to do that.

In fact, I believe the implementation of dynamic does use reflection internally anyway, at least partially, although a lot of it has been moved into the new DLR.
Mike Popoloski | Journal | SlimDX
Telastyn
Telastyn
Quote:
Original post by Mike.Popoloski
You can constrain your generic types to specific interfaces, so you could define an interface for types that expose Value and then call it on that.


Which sucks, because it requires explicit inheritance.

Quote:

C# 4.0 promises to introduce a dynamic type, which will support the concept of duck typing, allowing binding to be done at runtime.


Which isn't really designed for this sort of thing (though will invariably be abused to this sort of thing, like some use var everywhere now...).


Verg: C# is not C++. What you're trying to do is probably better done differently in the new language. What are you trying to do specifically?

Verg
Verg
Quote:
Original post by Telastyn
Quote:
Original post by Mike.Popoloski
You can constrain your generic types to specific interfaces, so you could define an interface for types that expose Value and then call it on that.


Which sucks, because it requires explicit inheritance.

Quote:

C# 4.0 promises to introduce a dynamic type, which will support the concept of duck typing, allowing binding to be done at runtime.


Which isn't really designed for this sort of thing (though will invariably be abused to this sort of thing, like some use var everywhere now...).


Verg: C# is not C++. What you're trying to do is probably better done differently in the new language. What are you trying to do specifically?


Just save some typing. The method I'm calling has the exact same signature and code inside of it, except that the T type passed in is different.

I noticed (while creating both methods) that they simplified down and were exactly the same, except for the passed in parameter type.

Again, it would be easy to extract a common base from that class... but it's not my code.


Chad

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.