【问题标题】:Calling a method group调用方法组
【发布时间】:2009-03-16 16:35:28
【问题描述】:

我有一个方法组,其中包含以下元素:

class Foobar
{
    public static DataSet C(out string SpName)
    {
        SpName = "p_C";
        return null;
    }

    public static DataSet C()
    {
        string SpName;
        C(out SpName);
        return DataAccess.CallSp( SpName);

    }
}

而我想做的是

 ButtonC.Text = DataAccess.GetSpName(**?????** Foobar.C )

我想在哪里执行此操作:

 public string GetSpName(**(?????)** method)
 {
     string spName = string.Empty;
     method(out spName);
     return spName;
 }

我已经尝试了各种项目,如?????没有成功。我错过了一些要点:-(

【问题讨论】:

    标签: c# .net methods delegates


    【解决方案1】:

    你需要声明一个delegate:

    // A delegate that matches the signature of
    // public static DataSet C      (out string SpName)
    public delegate  DataSet GetName(out string name);
    
    public class DataAccess
    {
       // ...
    
       static public string GetSpName(GetName nameGetter)
       {
           // TODO: Handle case where nameGetter == null
           string spName;
           nameGetter(out spName);
           return spName;
       }
    
       // ...
    }
    
    // ...
    
    public void SomeFunction()
    {
        // Call our GetSpName function with a new delegate, initialized
        // with the function "C"
        ButtonC.Text = DataAccess.GetSpName(new GetName( Foobar.C ))
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-22
      • 2012-09-30
      • 2018-06-24
      • 1970-01-01
      • 2013-01-17
      • 2017-08-10
      相关资源
      最近更新 更多