RE: Generics - A question on generics - delegates - runtime binding.
- From: "DanGo" <DanGo@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 22 Jul 2005 15:07:02 -0700
Hey bigguy!
The problem is that A<T> can be instantiated with any Type T. Your B
overloads GetValue for some distinct A<T>'s: A<int>, A<float>, A<string>,
A<SomeType> but there will always be some type T (for example FooBar) that
it does not overload. Generic classes must be able to work for any T you
want to pameterize them with.
This seems to make them nearly useless since the only operations we can
perform on any type T are the ones in the common base class "object"
Enter constraints.
Constraints let us add operations that T can perform by constraining the
type of T
E.g
public class Foo<T> where T : IBar
So now we can use any operations that the IBar interface supports and in
turn we promise that whenever we instantiate a Foo we will pass a T that
implements IBar.
Now the bad news.
Interfaces don't support static members as part of their signature. So we
have no way to constrain the type to say that we will only try to create A<T>
with types that implement T GetValue(A<T>).
So what are you really trying to do?
"bigtexan" wrote:
> I would like to do the following and cannot figure it out.
>
> public class A<T>
> {
> public delegate T GetValueDelegate(A<T> var);
> public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);
> }
>
> public class B
> {
> public static int GetValue(A<int> var)
> {
> //do something and return int
> }
>
> public static float GetValue(A<float> var)
> {
> //do something and return float
> }
>
> public static string GetValue(A<string> var)
> {
> //do something and return string
> }
>
> //or in general
> public static SomeType GetValue(A<SomeType> var)
> {
> //do something specific for type SomeType and return that SomeType
> }
> }
>
> Now:-----------------------------------
> I was thinking when I do
>
> ...somewhere in some code....
> A<int> s = new A<int>();
>
> that on creation of this instance of A of type int that the delegate
> in A would find the proper overload in class B such that:
> ...
> int I = s.GetValue();
> ...
> would resolve to the class B's method
> ...
> public static int GetValue(A<int> var)
> ...
> and this would occur dynamically at runtime.
>
>
> The short is that I get a 'No Overload Error' as follows.
> 'No overload for 'GetValue' matches delegate 'A<T>.GetValueDelegate'
>
> I am obviously not understanding something. Help would be greatly appriciated.
>
> k
>
>
> --
> A single 20-watt compact fluorescent lamp used in place of a 75-watt
> incandescent will save about 550 kilowatt-hours over its lifetime. That is
> 500 pounds of coal; a pile the size of your office desk.
.
- Follow-Ups:
- References:
- Prev by Date: How can I pass a multidimensional array as a ref parameter in func
- Next by Date: Re: How can I pass a multidimensional array as a ref parameter in func
- Previous by thread: Re: Generics - A question on generics - delegates - runtime binding.
- Next by thread: Re: Generics - A question on generics - delegates - runtime binding.
- Index(es):
Relevant Pages
|