2011-04-20 81 views
4

我的代码如下:委托错误

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”函数,并且它们都接受字符串输入?

在此先感谢...

回答

3

在代码中有两个问题。

  • 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); 
+0

有一个例外:错误绑定到目标方法:-( – seoul 2011-04-20 12:00:05

+0

@seoul:看到答案更新 – 2011-04-20 12:23:45

+0

详细信息和有用的答案:-)谢谢... – seoul 2011-04-21 03:21:02

0

你不能调用Delegate类型的方法。你必须使用DynamicInvoke()这是非常slowwwwwww

试试这个:

string output = (string) del.DynamicInvoke(new object[]{"asldkl"}); 
0

您正在使用Delegate类不是delegate关键字。

0

只有调用具有已知签名的方法调用语法,才能调用委托。您需要将您的委托转换为您之前定义的委托类型。

var del = (PropertyRetrievalClass.getProperty)Delegate.CreateDelegate(typeof(PropertyRetrievalClass.getProperty), obj, "get_Chart_" + i.ToString()); 

你还需要的第一个参数更改为CreateDelegate,因为它应该是委托类型。并在“get_Chart_”中大写“C”。

然后,你将需要转换返回objectstring

string output= (string) del("asldkl"); 

或更改委托类型和方法有string作为他们的返回类型。

+1

的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

1

其他答案已经解决了您的代码的问题,但我想提供一个替代方案。

如果有那你的检索类是从选择的方法有限,数量有限,并且它们具有相同的特征,这可以更有效地完成,而无需使用反射:

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 
} 
+0

严...你也许解决方案另外如果我不强制只使用委托。 – seoul 2011-04-21 03:21:55

+0

+1也适合您的想法。 – seoul 2011-04-21 03:23:02