调用方法组

时间:2009-03-16 16:35:29

标签: c# .net methods delegates

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

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;
 }

我尝试过各种各样的物品?????没有成功。我错过了一些好点: - (

1 个答案:

答案 0 :(得分:2)

您需要申报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 ))
}