委托错误
我的代码如下:委托错误
class PropertyRetrievalClass
{
public delegate object getProperty(string input);
public object get_Chart_1(string iput)
{
Console.WriteLine(iput);
return "";
}
public object get_Chart_2(string iput)
{
Console.WriteLine(iput);
return "";
}
public PropertyRetrievalClass() { }
}
public static void Main()
{
int i = 1;
PropertyRetrievalClass obj = new PropertyRetrievalClass();
Delegate del = Delegate.CreateDelegate(typeof(PropertyRetrievalClass), obj, "get_chart_" + i.ToString());
string output= del("asldkl");
}
这是给我一个错误说“错误CS0118:‘德尔’是‘变量’,而是使用类似‘法’”
我该怎么做才能使用这个代表?我想调用任何“get_chart_1”或“get_chart_2”函数,并且它们都接受字符串输入?
在此先感谢...
在代码中有两个问题。
- 甲
Delegate
对象不是方法,所以需要使用Delegate
对象上的方法调用它是指该方法 - 的第一个参数
CreateDelegate
应该委托类型,不含有所述类你想要调用的方法。
全部工作示例:
public delegate void ParamLess();
class SomeClass
{
public void PrintStuff()
{
Console.WriteLine("stuff");
}
}
internal class Program
{
private static Dictionary<int, int> dict = null;
static void Main()
{
var obj = new SomeClass();
Delegate del = Delegate.CreateDelegate(typeof(ParamLess), obj,
"PrintStuff", false);
del.DynamicInvoke(); // invokes SomeClass.PrintStuff, which prints "stuff"
}
}
在你的情况下,Main
方法应该是这样的:
public static void Main()
{
int i = 1;
PropertyRetrievalClass obj = new PropertyRetrievalClass();
Delegate del = Delegate.CreateDelegate(
typeof(PropertyRetrievalClass.getProperty),
obj,
"get_Chart_" + i.ToString());
string output = (string)del.DynamicInvoke("asldkl");
}
更新
注意CreateDelegate
是区分的方法敏感名称,除非你不告诉它。
// this call will fail, get_chart should be get_Chart
Delegate del = Delegate.CreateDelegate(
typeof(PropertyRetrievalClass.getProperty),
obj,
"get_chart_" + i.ToString());
// this call will succeed
Delegate del = Delegate.CreateDelegate(
typeof(PropertyRetrievalClass.getProperty),
obj,
"get_Chart_" + i.ToString());
// this call will succeed, since we tell CreateDelegate to ignore case
Delegate del = Delegate.CreateDelegate(
typeof(PropertyRetrievalClass.getProperty),
obj,
"get_chart_" + i.ToString(),
true);
你不能调用Delegate
类型的方法。你必须使用DynamicInvoke()
这是非常slowwwwwww。
试试这个:
string output = (string) del.DynamicInvoke(new object[]{"asldkl"});
您正在使用Delegate
类不是delegate
关键字。
只有调用具有已知签名的方法调用语法,才能调用委托。您需要将您的委托转换为您之前定义的委托类型。
var del = (PropertyRetrievalClass.getProperty)Delegate.CreateDelegate(typeof(PropertyRetrievalClass.getProperty), obj, "get_Chart_" + i.ToString());
你还需要的第一个参数更改为CreateDelegate
,因为它应该是委托类型。并在“get_Chart_”中大写“C”。
然后,你将需要转换返回object
到string
:
string output= (string) del("asldkl");
或更改委托类型和方法有string
作为他们的返回类型。
的getProperty不是主要可见。所以编辑这种方式 - > PropertyRetrievalClass.getProperty del =(PropertyRetrievalClass.getProperty)Delegate.CreateDelegate(typeof(PropertyRetrievalClass),obj,“get_chart_”+ i.ToString()); string output = new del(“asldkl”); ************** 它不工作,要么...我得到这个错误 - >中的类型或命名空间名称“德尔”找不到(是否缺少使用指令或程序集引用) – seoul 2011-04-20 11:54:52
其他答案已经解决了您的代码的问题,但我想提供一个替代方案。
如果有那你的检索类是从选择的方法有限,数量有限,并且它们具有相同的特征,这可以更有效地完成,而无需使用反射:
public int MethodIndex {get;set;}
public static void Main()
{
PropertyRetrievalClass obj = new PropertyRetrievalClass();
Func<string,object> getChartMethod;
switch(MethodIndex)
{
case 1:
getChartMethod = obj.get_chart_1;
break;
case 2:
getChartMethod = obj.get_chart_2;
break;
}
string output= getChartMethod("asldkl");
}
如果有很多,你可以创建一个数组而不是使用开关。很明显,你可以直接从交换机运行适当的函数,但我认为这个想法是你可能想要将委托传递给调用者,而像这样的构造可以让你在不使用反射的情况下执行,例如
public static Func<string,object> GetMethod
{
... just return getChartMethod directly
}
有一个例外:错误绑定到目标方法:-( – seoul 2011-04-20 12:00:05
@seoul:看到答案更新 – 2011-04-20 12:23:45
详细信息和有用的答案:-)谢谢... – seoul 2011-04-21 03:21:02