C# 在运行时使用委托选择函数

C# 在运行时使用委托选择函数,c#,delegates,C#,Delegates,我是C#的新手,仍然理解代表的概念。我对委托的了解是,委托定义了一个函数签名,具有相同签名的函数可以添加到该委托中 public class ss { public delegate void sampleDelegate(); public ss() { sampleDelegate s1 = new sampleDelegate(sampleMethod); s1+= new sampleDelegate(sampleMethod2)

我是C#的新手,仍然理解代表的概念。我对委托的了解是,委托定义了一个函数签名,具有相同签名的函数可以添加到该委托中

public class ss
{
    public delegate void sampleDelegate();

    public ss()
    {
        sampleDelegate s1 = new sampleDelegate(sampleMethod);
        s1+= new sampleDelegate(sampleMethod2);
        s1();
    }


    public static void sampleMethod()
    {

    }
    public static void sampleMethod2()
    {

    }
}
在上面的代码中,我创建了一个委托,并将其命名为sampleMethod和sampleMethod2。当我调用s1()时,它同时调用sampleMethod()和sampleMethod2()

如果我只想调用其中一个方法,而这个决定是在运行时做出的,该怎么办

我可能遗漏了一些非常小的内容,但代表们真的很难理解

如果我只想调用其中一个方法,而这个决定是在运行时做出的,该怎么办

基本上,你不会把它们结合在一起。例如:

// Names changed to be more conventional

SampleDelegate s1 = someCondition
    ? new SampleDelegate(SampleMethod)
    : new SampleDelegate(SampleMethod2);
// This will call either SampleMethod or SampleMethod2, depending on condition
s1(); 
请注意,通常我会使用方法组转换,但不能对条件运算符的第二个和第三个操作数使用方法组。您可以使用:

SampleDelegate s1;
if (condition) {
    s1 = SampleMethod;
} else {
    s2 = SampleMethod2;
}
。。。或

SampleDelegate s1 = someCondition
    ? new SampleDelegate(SampleMethod)
    : SampleMethod2;

不过,这两种方法对我来说都不是特别好。

委托就像指向方法的指针。在运行时,除了方法是对象的成员之外,调用委托或方法没有任何区别:

some.Method();
someDelegate();
委托的目标是创建一个黑盒,在那里您希望一些代码放置一些行为,而您只需要依赖一个方法签名

归根结底,它们就像方法接口:

也就是说,一个方法不能有条件地与其他方法切换,但您需要使用常规的控制流块,如
if
switch
来决定要做什么

为了避免重复Jon Skeet的答案,我将添加另一种可能的解决方案:

    string text = "";

    sampleDelegate s1 = () =>
    {
         if(!string.IsNullOrEmpty(text)) 
         {
             SampleMethod1();
         }
         else
         {
             SampleMethod2();
         }
    };

    s1();
检查是否可以创建一个处理条件逻辑的实例来调用
SampleMethod1
SampleMethod2
,而不是创建两个
sampleDelegate
实例

整个
()=>{}
过程称为匿名委托

public class ss
{
    public delegate void sampleDelegate();

    public ss()
    {
        sampleDelegate s1 = new sampleDelegate(sampleMethod);
        s1+= new sampleDelegate(sampleMethod2);
        s1();
    }


    public static void sampleMethod()
    {

    }
    public static void sampleMethod2()
    {

    }
}
也可以表示为:

    string text = "";

    sampleDelegate s1 = delegate()
    {
         if(!string.IsNullOrEmpty(text)) 
         {
             SampleMethod1();
         }
         else
         {
             SampleMethod2();
         }
    };
但是不要使用上面的语法。。。它来自旧的.NET时代(.NET 1.0/1.1/2.0)

总之,委托可以作为实际的类方法提供,也可以不作为实际的类方法提供,但是您可以将类方法与匿名委托结合起来以满足您的需要

如果我只想调用其中一个方法,而这个决定是 要在运行时生成?我可能错过了一些非常小的东西,但是 学员们真的很难理解

实际上,这就是使用委托的原因,但是你说你遗漏了什么是对的

学员习惯于以下情况:

public void DoStuff(string text, Func<string, string> textFormatter = null)
{
    Console.WriteLine(textFormatter != null ? textFormatter(text) : text);
}

因此,您的意思是,如果没有if/else或switch case,就无法使用委托来选择要运行的特定方法?@AnoopDV:我不确定您在问什么。你必须在某个地方使用条件,是的。。。您还希望如何在多个选项中进行选择?这种情况可以用多种方式表达。。。
public void DoStuff(string text, Func<string, string> textFormatter = null)
{
    // We'll give a default formatter if none is provided ;)
    if(textFormatter == null)
      textFormatter = text => $"<span>{text}</span>";

    Console.WriteLine(textFormatter(text));
}