【.NET基础加强第九课--事件】

Event 关键字

委托,事件对比

委托是一种数据类型,可以用=调用。
事件:对象(对委托的一种封闭装)。只能在类内部,只能通过+=,或-=调用。 事件的内部是用委托实际 的。

举例: 音乐播放事件

MusicPlayer musicPlayer = new MusicPlayer();
musicPlayer.AfterStart = () =>
{
global::System.Console.WriteLine(“加载歌词”);
};
musicPlayer.StartPlay();
musicPlayer.BeforeStop = () =>
{
global::System.Console.WriteLine(“删除歌词”);
};
musicPlayer.EndMusic();

// 委托可以用=直接赋值,可以将以前注册的方法都覆盖掉
// 事件在外部不能直接调用
musicPlayer = null;

Console.ReadKey();

public class MusicPlayer
{
public Action AfterStart;
public Action BeforeStop;
private void PlayMusic()
{
Console.WriteLine(“播放音乐”);

}

public void StartPlay()
{
    PlayMusic();
    if(AfterStart != null)
    {
        AfterStart();
    }
    Thread.Sleep(2000);
}

public void EndMusic()
{
    if(BeforeStop != null)
    {
        BeforeStop();
    }
    Console.WriteLine("播放完结");
}

}

用户对象,登录

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    int count = 0;
    private void button1_Click(object sender, EventArgs e)
    {
        count++;

    }

    private void uControlButtonExt1_Load(object sender, EventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //uControlButtonExt1.TripleClick = () =>
        //{
        //    MessageBox.Show("hello print");
        //};
        ucLogin1.UserLoginValidating += new Action<object, UserLoginEventArgs>(ucLogin1_UserLogin);
    }

    // 事件校验方法
    void ucLogin1_UserLogin(object sender, UserLoginEventArgs e)
    {
        if(e.LoginId == "admin")
        {
            e.IsOk = true;  
        }  
    }

    private void uControlButtonExt21_Load(object sender, EventArgs e)
    {
        // 事件不能用=赋值,只能用+=或-=赋值
        uControlButtonExt21.Tripleclick += () =>
         {
             MessageBox.Show("事件");
         };
    }

    private void ucLogin1_Load(object sender, EventArgs e)
    {

    }
}

事件窗体

程序集

程序集包含:类型元数据,程序集元数据,IL代码,资源文件

反射

动态获取程序集,并获取类中的信息,

// 绝对路径
Assembly assembly = Assembly.LoadFile(@“D:\MVC\MVCDemo\文件操作\bin\Debug\net6.0\文件操作.dll”);

1, 获取该程序集中的所有类型
Type[] types = assembly.GetTypes();
for (int i = 0; i < types.Length; i++)
{
Console.WriteLine(types[i].FullName);
}

// 2, 获取所有的public的类型
Type[] types = assembly.GetExportedTypes();
for(int i = 0; i < types.Length; i++)
{
Console.WriteLine(types[i].FullName);
}

获取指定的类型
Type type = assembly.GetType(“Flyable”);
MethodInfo method = type.GetMethod(“Eat”);
根据指定的Type,创建一个该类型的对象
object obj = Activator.CreateInstance(type);
method.Invoke(obj, null);

Type type = assembly.GetType(“Flyable”);
调用重载就是通过第二个Type[] 参数来区分
MethodInfo method = type.GetMethod(“Eat”, new Type[]
{
typeof(string)
});

调用该重载方法
method.Invoke(Activator.CreateInstance(type),
new object[] { “吃得好!”});

object obj = Activator.CreateInstance(type);
// 通过Type 来创建对象==============
// 通过调用指定的构造函数 来创建对象
ConstructorInfo info = type.GetConstructor(new Type[] { typeof(string) });
// 调用构造函数创建对象
object obj = info.Invoke(new object[] { “张三” });

// 通过反射获取 指定对象的属性的值
PropertyInfo pinfo = type.GetProperty(“Name”);
string name = pinfo.GetValue(obj,null).ToString();
Console.WriteLine(name);

Console.ReadKey();

class MyType
{
public int Id { get; set; }
public string[] boys;
public string Name { get; set; }
public void Say()
{
Console.WriteLine(“Say what”);
}

private void Do()
{
    Console.WriteLine("DO what");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yuansheng888888

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值