泛型 - 关于泛型的问题 - 委托 - 运行时绑定。 [英] Generics - A question on generics - delegates - runtime binding.
问题描述
我想做以下事情并且无法理解。
公共等级A< T>
{
public delegate T GetValueDelegate(A< T> var);
public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);
}
公共类B
{
public static int GetValue(A< int> var)
{
//做某事并返回int
}
public static float GetValue(A< float> var)
{
//做一些事情并返回浮动
}
公共静态字符串GetValue(A< string> var)
{
//做点什么并返回字符串
}
//或一般
public static SomeType GetValue(A< SomeType> var)
{
//为SomeType类型执行特定操作并返回SomeType
}
}
现在:----------------------- ------------
当我在某些代码的某处时,我正在思考
....
A< int> s = new A< int>();
在创建此类型为A的实例时,A中的委托
会找到合适的B级过载,这样:
....
int I = s.GetValue();
....
将解析为B类的方法
....
public static int GetValue(A< int> var)
....
这会在运行时动态发生。
简短的说法是我得到一个''No Overload Error'',如下所示。
'''GetValue''没有超载匹配委托''< T> .GetValueDelegate''
我显然不理解某事。帮助将大大减少。
k
-
一个20瓦的紧凑型荧光灯用来代替75 -watt
白炽灯在其使用寿命期间可节省约550千瓦时。那是
500磅煤;办公桌大小的堆。
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.
推荐答案
你没有为B类中的GetValue定义一个不带
$ b的重载$ b参数。但是,你在A级< T>来称呼它。
" bigtexan" <双****** @ online.nospam>在消息中写道
news:37 ********************************** @ microsof t.com ...
我想做以下事情并且无法理解。
公共类A< T>
{
公共代表T GetValueDelegate(A< T> var);
public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);
}
公共类B
{
public static int GetValue(A< int> var)
{
//做点什么并返回int
}
公共静态浮点数GetValue(A< float> var)
{
//做点什么并返回浮动
}
公共静态字符串GetValue(A< string> var)
{
//做点什么并返回字符串
}
//或一般
公共静态SomeType GetValue(A< SomeType> var)
{
//做一些特定的事情键入SomeType并返回Some输入
}
}
现在:----------------- ------------------
当我做的时候我在想
....一些代码....
A< int> s = new A< int>();
在创建此类型为A的实例时,A中的委托
会找到合适的B级过载,这样:
....
int I = s.GetValue();
....
将解析为B类的方法
....
public static int GetValue(A< int> var)
....
这会在运行时动态发生。
简短的说法是我得到一个''No Overload Error'',如下所示。
'''GetValue''没有超载匹配委托''< T> .GetValueDelegate''
我显然不理解某事。帮助将是非常的
appriciated。
k
-
单个20瓦紧凑型用于代替75瓦白炽灯的荧光灯在其使用寿命期间可节省约550千瓦时。那是
500磅煤;办公桌大小的堆。
You have not defined a overload for GetValue in class B that takes no
parameters. But, you are calling it in class A<T>.
"bigtexan" <bi******@online.nospam> wrote in message
news:37**********************************@microsof t.com...
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.
你没有为B类中的GetValue定义一个不带
参数的重载。但是,你在A级< T>来称呼它。
" bigtexan" <双****** @ online.nospam>在消息中写道
news:37 ********************************** @ microsof t.com ...
我想做以下事情并且无法理解。
公共类A< T>
{
公共代表T GetValueDelegate(A< T> var);
public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);
}
公共类B
{
public static int GetValue(A< int> var)
{
//做点什么并返回int
}
公共静态浮点数GetValue(A< float> var)
{
//做点什么并返回浮动
}
公共静态字符串GetValue(A< string> var)
{
//做点什么并返回字符串
}
//或一般
公共静态SomeType GetValue(A< SomeType> var)
{
//做一些特定的事情键入SomeType并返回Some输入
}
}
现在:----------------- ------------------
当我做的时候我在想
....一些代码....
A< int> s = new A< int>();
在创建此类型为A的实例时,A中的委托
会找到合适的B级过载,这样:
....
int I = s.GetValue();
....
将解析为B类的方法
....
public static int GetValue(A< int> var)
....
这会在运行时动态发生。
简短的说法是我得到一个''No Overload Error'',如下所示。
'''GetValue''没有超载匹配委托''< T> .GetValueDelegate''
我显然不理解某事。帮助将是非常的
appriciated。
k
-
单个20瓦紧凑型用于代替75瓦白炽灯的荧光灯在其使用寿命期间可节省约550千瓦时。那是
500磅煤;办公桌大小的堆。
You have not defined a overload for GetValue in class B that takes no
parameters. But, you are calling it in class A<T>.
"bigtexan" <bi******@online.nospam> wrote in message
news:37**********************************@microsof t.com...
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.
感谢您的回复,但我不认为这是原因。 A类中的代表
是使用签名定义的。这个签名应该与B类中的方法相匹配.B中的方法匹配。
公共委托T GetValueDelegate(A< T> ; var);
谢谢,但我仍然遗漏了一些东西。
Thank you for the reply but I do not think that is the reason. The delegate
in class A is defined with the signature. It is this signature that should
match against the methods in class B. And the methods in B do match.
public delegate T GetValueDelegate(A<T> var);
thanks, but I am still missing something.
这篇关于泛型 - 关于泛型的问题 - 委托 - 运行时绑定。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!