在 C# 中使用委托进行动态方法调用

89da51678cfe20d270a06a53b03f6a7e.jpeg在 C# 中,委托是一个强大的概念,它允许我们将方法视为值。这意味着我们可以将方法分配给变量,将它们作为参数传递给其他方法,并动态调用它们。在这篇文章中,我将通过示例实现讨论从简单到高级的委托详细信息。

代表提供几个关键优势:

  • 改进的代码组织: 通过将方法定义与其调用点分开,委托可以促进更清晰的代码结构。

  • 方法调用的灵活性: 委托提供了一种机制,用于在运行时根据某些条件或事件调用方法。

  • 活动处理: 委托是在 C# 中处理事件的基础,可实现对象之间的通信。

了解委托

定义委托:语法和关键组件

委托声明定义了一种类型,该类型引用具有特定签名的方法。语法如下:

public delegate return_type delegate_name(parameter_list);
  • return_type:委托引用的方法返回的值类型(例如,、、)。

  • delegate_name:您为代理类型指定的名称。

  • parameter_list:委托引用的方法可以接受的参数的逗号分隔列表(对于没有参数的方法,可以为空)。

例如,以下委托声明定义了一个委托,该委托将字符串作为输入并返回修改后的字符串:StringModifier

public delegate string StringModifier(string inputString);

创建委托实例:分配方法

若要创建委托对象,请使用关键字,后跟委托类型和方法引用。方法如下:

StringModifier modifyString; // Declare a delegate variable  
  
// Assign a method to the delegate  
modifyString = (str) => str.ToUpper();

该变量现在包含对 lambda 表达式的引用。

通过委托调用方法

调用委托

要调用与委托关联的方法,只需调用委托变量名称,后跟括号。这就像调用任何其他方法一样:

string modifiedText = modifyString("Hello World"); // Call the delegate  
Console.WriteLine(modifiedText); // Output: HELLO WORLD

组播代表

委托可以是多播的,这意味着单个委托变量可以保存对具有兼容签名的多个方法的引用。我们可以使用 and 运算符链接方法。链式方法按后进先出 (LIFO) 顺序调用:++=

using System;  
  
class Program  
{  
    public delegate int MathOperation(int a, int b);  
  
    public static void Main(string[] args)  
    {  
        int Add(int x, int y) => x + y;  
        int Multiply(int x, int y) => x * y;  
  
        MathOperation operation;  
  
        operation = Add;  
        operation += Multiply;  
  
        int result = operation(5, 3);  
        Console.WriteLine(result);  
    }  
}

代表的高级应用程序

事件处理

事件是对象在特定条件发生时引发的通知。其他对象可以使用委托订阅(注册)这些事件,从而允许它们在事件发生时收到通知并执行特定代码:

using System;  
  
public class Button  
{  
    // Define a delegate for the click event  
    public delegate void ClickedEventHandler(string buttonName);  
  
    // Define an event of type ClickedEventHandler  
    public event ClickedEventHandler Clicked;  
  
    public string Name { get; private set; }  
  
    public Button(string name)  
    {  
        Name = name;  
    }  
  
    // Method to simulate a button click  
    public void Click()  
    {  
        // Check if any event handlers are subscribed  
        if (Clicked != null)  
        {  
            // Invoke all subscribed event handlers and pass the button name  
            Clicked(Name);  
        }  
    }  
}  
  
class Program  
{  
    public static void Main(string[] args)  
    {  
        // Create two instances of the Button class with different names  
        Button button1 = new Button("Button 1");  
        Button button2 = new Button("Button 2");  
  
        // Subscribe to the click event of button1  
        button1.Clicked += (buttonName) => Console.WriteLine(buttonName + " clicked!");  
  
        // Subscribe to the click event of button2  
        button2.Clicked += (buttonName) => Console.WriteLine(buttonName + " clicked!");  
  
        // Simulate a click on button1  
        button1.Click();  
  
        // Simulate a click on button2  
        //button2.Click();  
    }  
}

Lambda 表达式

Lambda 表达式提供了一种简洁的方式来定义内联的匿名方法。它们可以在委托分配中使用,提供更紧凑的方法:

StringModifier modifyString = str => str.ToUpper(); // Lambda expression for uppercase  
  
int operation = (x, y) => x * y + 3; // Lambda for chained math operation

函数和操作

命名空间提供预定义的委托类型n

  • Func:表示一个函数,该函数采用零个或多个参数并返回一个值。

Func<int, int, string> formatNumber = (x, y) => $"The number is: {x * y}";
  • Action:表示采用零个或多个参数但不返回值 (void) 的方法。

Action logMessage = () => Console.WriteLine("Message logged!");

最佳做法和注意事项

签名匹配和重载

确保委托和方法之间严格的签名兼容性至关重要。将方法分配给委托时,参数的数量、顺序和类型必须完全匹配。

在处理委托时,方法重载可能会造成混淆。如果一个类中的多个方法具有相同的名称但参数列表不同,编译器将根据委托人的签名选择最合适的方法。

Delegate Safety

空引用: 调用没有赋值方法(空引用)的委托将抛出一个 .在调用之前检查 null。

方法不兼容: 如果调用的方法的签名与委托的期望不匹配,则可能会发生编译错误或运行时异常。在分配具有不同参数类型的方法时要小心。

结论

我们已经探讨了有关委托的核心概念、创建、调用和高级应用程序。记得:

  • 委托允许将方法视为值。

  • 它们促进了更清晰的代码结构和动态的方法调用。

  • 委托是 C# 中事件处理的基础。

  • 将 lambda 表达式用于带有委托的简洁匿名函数。

  • 对于常见方案,请考虑预定义的委托,例如 和。FuncAction

  • 请注意签名匹配和代表安全性,以实现可靠的代码。

  • 如果你喜欢我的文章,请给我一个赞!谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值