C#语言进阶(二)—事件 第二篇(.net标准事件模型)

总目录
C# 语法总目录

系列链接
C#语言进阶(二)    事件 第一篇(发布订阅模式)
C#语言进阶(二)    事件 第二篇(.net标准事件模型)
C#语言进阶(二)    事件 第二篇(事件访问器)

事件 第二篇

2. .net标准事件模型

  标准事件模型是 .net framwork 定义的一个标准。可用可不用,只是一个标准而已。

  官方为这个标准定义了一个事件参数类,用于给事件传递参数。这就是上面说的,这个模型可用可不用,不用官方的,自己也能做一个类似的,做个开发,没必要搞得这么复杂。

以下是上面案例根据标准事件模型的修改版本。

  这里使用 .net framwork的标准事件模型参数类: System.EventArgs 类,来模拟标准事件模型

标准事件参数类

//继承标准事件模型参数类型
//这个父类啥都没有,只有一个静态参数,一个构造方法,可以点进去看
public class ScoreChangedEventArgs : EventArgs
{
    public static readonly new ScoreChangedEventArgs? Empty;
    //通常标准事件模型传递的参数设置为只读类型
    public readonly decimal oldScore;
    public readonly decimal newScore;
    public ScoreChangedEventArgs(decimal oldScore,decimal newScore)
    {
        this.oldScore = oldScore;
        this.newScore = newScore;
    }
}

发布者类

//发布者
public class BroadCasterStandar
{
    private string? name;
    private decimal score;
    //事件标准委托
    public event EventHandler<ScoreChangedEventArgs>? ScoreChanged;

    protected virtual void OnScoreChanged(ScoreChangedEventArgs? e)
    {
        ScoreChanged?.Invoke(this, e);
    }

    public BroadCasterStandar(string name)
    {
        this.name = name;
    }
    public decimal Score
    {
        get { return score; }
        set
        {
            if (score == value) return;
            decimal oldScore = score;
            score = value;
            
            OnScoreChanged(new ScoreChangedEventArgs(oldScore, score));
            //如果不需要传值,那么可以用下面代替
            //OnScoreChanged(ScoreChangedEventArgs.Empty);
        }
    }
}

订阅者类

//订阅者
internal class SubscriberStandar
{
    private readonly string _id;
    public SubscriberStandar(string id, BroadCasterStandar broad)
    {
        _id = id;
        //订阅信息
        broad.ScoreChanged += ScoreChanged;
    }

    //处理广播信息
    void ScoreChanged(object? obj, ScoreChangedEventArgs e)
    {
        if (e == ScoreChangedEventArgs.Empty)
        {
            return;
        }
        Console.WriteLine("this id is: " + _id + ",  oldscore is " + e.oldScore + "  ,new Score is: " + e.newScore + "  ,time is: " + DateTime.Now);
    }
}

主程序

static void Main(string[] args)
{
    BroadCasterStandar bcs = new BroadCasterStandar("bcs");
    SubscriberStandar sbs1 = new SubscriberStandar("01", bcs);
    SubscriberStandar sbs2 = new SubscriberStandar("02", bcs);
    //广播信息
    bcs.Score = 15;
}

输出

//输出
this id is: 01,  oldscore is 0  ,new Score is: 15  ,time is: 2000/1/1 16:43:12
this id is: 02,  oldscore is 0  ,new Score is: 15  ,time is: 2000/1/1 16:43:12

总目录
C# 语法总目录

系列链接
C#语言进阶(二)    事件 第一篇(发布订阅模式)
C#语言进阶(二)    事件 第二篇(.net标准事件模型)
C#语言进阶(二)    事件 第二篇(事件访问器)

相关推荐

  1. C#语言()—事件 第二(.net标准事件模型)

    2024-04-28 22:20:01       33 阅读
  2. C#语言()—事件 第二(.net标准事件模型)

    2024-04-28 22:20:01       36 阅读
  3. C#语言()—事件 第三(事件访问器)

    2024-04-28 22:20:01       35 阅读
  4. C#语言(一)—委托 第二

    2024-04-28 22:20:01       32 阅读
  5. C#语言(一)—委托 第一

    2024-04-28 22:20:01       28 阅读
  6. C#基础语法( 委托与事件

    2024-04-28 22:20:01       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-28 22:20:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-28 22:20:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-28 22:20:01       87 阅读
  4. Python语言-面向对象

    2024-04-28 22:20:01       96 阅读

热门阅读

  1. 安卓如何搜索到蓝牙5.0的扩展广播

    2024-04-28 22:20:01       30 阅读
  2. 基于微信小程序的旅游系统的设计与实现

    2024-04-28 22:20:01       34 阅读
  3. 整点秒杀功能需求说明书

    2024-04-28 22:20:01       32 阅读
  4. php中常见的函数和使用方法

    2024-04-28 22:20:01       23 阅读
  5. 【深度学习】图像修复的一些模型

    2024-04-28 22:20:01       25 阅读
  6. k8s部署prometheus

    2024-04-28 22:20:01       34 阅读
  7. Git添加空文件夹

    2024-04-28 22:20:01       29 阅读
  8. C#面:IEnumerable的缺点有哪些

    2024-04-28 22:20:01       34 阅读
  9. Codeforces Round 941 (Div. 2) ABC

    2024-04-28 22:20:01       38 阅读
  10. RN传入数字返回拼音首字母的包

    2024-04-28 22:20:01       32 阅读