C#面向对象22 委托事件反射
1.委托的定义:声明委托类型(返回值和参数,命名空间中);定义委托对象
(把委托想象成函数中的占位符~因为你并不确定调用哪个函数~)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace @delegate { public delegate void DelSayHi(string name); class Program { static void Main(string[] args) { //1.委托的定义:声明委托类型(返回值和参数);定义委托对象; //DelSayHi del = SayChinese; //del("22"); //DelSayHi del = delegate(string name){Console.WriteLine("吃了么?" + name);}; //del("22"); //DelSayHi del = (name) => { Console.WriteLine("吃了么?" + name); }; //del("112233"); //2 Test("22", SayChinese); //3 匿名函数 Test("222", delegate(string name) { Console.WriteLine("吃了么?" + name); }); //4 lamda表达式 Test("222", (name) => { Console.WriteLine("吃了么?" + name); }); Console.ReadKey(); } public static void Test(string name, DelSayHi del) { //调用 del(name); } public static void SayChinese(string name) { Console.WriteLine("吃了么?" + name); } public static void SayEnglish(string name) { Console.WriteLine("Nice to meet you" + name); } } }
2. 匿名函数 lamda表达式
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace delegate2 { public delegate string DelString(string name); class Program { static void Main(string[] args) { string[] names = { "abCDefG", "HIJKlmnOP", "QRsTuW", "XyZ" }; //转大写 lamda DealString(names, (name) => { return name.ToUpper(); }); //转大写 匿名函数 DealString(names, delegate(string name) { return name.ToUpper(); }); foreach (string item in names) { Console.WriteLine(item); } Console.ReadKey(); } static void DealString(string [] names ,DelString del2) { for (int i = 0; i < names.Length; i++) { names[i] = del2(names[i]); } } } }
3.求取任意数组的最大值
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace delegate3 { public delegate int DelCompare(object t1,object t2); class Program { static void Main(string[] args) { object[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; object[] strings = { "1", "12", "123" }; object[] persons = { new person(13,"22"),new person(14,"33"),new person(15,"44")}; //int 数组 object i =GetMax(nums,delegate(object max,object num){ int IntMax =(int)max; int IntNum=(int)num; int result=IntMax-IntNum; return result; }); Console.WriteLine(i); //string 数组 object j = GetMax(strings, (max, num) => { string s_max = (string)max; string s_num = (string)num; return s_max.Length - s_num.Length; }); Console.WriteLine(j); //对象 数组 object oldPserson = GetMax(persons, (max, num) => { person s_max = (person)max; person s_num = (person)num; return s_max.Age - s_num.Age; }); Console.WriteLine(((person)oldPserson).Name); Console.ReadKey(); } static object GetMax(object [] nums,DelCompare del) { object max = nums[0]; for (int i = 0; i < nums.Length; i++) { if (del(max,nums[i]) < 0) max = nums[i]; } return max; } } class person { public person(int _age, string _name) { Age = _age; Name = _name; } public int Age { get; set; } public string Name{get;set;} } }
4.泛型委托
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace delegate4 { public delegate int DelCompare<T>(T t1, T t2); class Program { static void Main(string[] args) { int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 99 }; int i = GetMax<int>(nums, (max, num) => { return max - num; }); Console.WriteLine(i); Console.ReadKey(); } static T GetMax<T>(T[] nums, DelCompare<T> del) { T max = nums[0]; for (int i = 0; i < nums.Length; i++) { if (del(max, nums[i]) < 0) max = nums[i]; } return max; } } }
5.窗体之间的传值
Form1.cs:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 fm2 = new Form2(); fm2.showtt = ShowLab; fm2.Show(); } private void ShowLab(string tt) { this.label1.Text = tt; } } }
Form2.cs:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public delegate void ShowDele(string tt); public partial class Form2 : Form { public ShowDele showtt; public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string temp = this.textBox1.Text.Trim(); showtt(temp); } } }
6.事件 (安全类型的委托!)
**事件只能在声明事件的那个类中被调用!类以外只能注册这个事件!
类中声明事件,调用事件:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace _06使用Winform应用程序模拟事件 { public delegate void DelPlayOver(); class PlayMusic { public event DelPlayOver Del;//声明事件不需要写() //音乐播放的名字 public string Name { get; set; } public PlayMusic(string name) { this.Name = name; } public void PlaySongs() { Console.WriteLine("正在播放" + this.Name); //模拟播放了三秒钟 Thread.Sleep(3000); if (Del != null) { //当播放完成后 执行一个事件 Del();//直接调用“事件” } } } }
类的外面,(另一个类中注册事件):
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace _06使用Winform应用程序模拟事件 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { PlayMusic p = new PlayMusic("忐忑");//创建对象 开始播放音乐 p.Del += Test;//注册事件 p.PlaySongs();//开始播放音乐 } //事件要执行的函数 void Test() { //Console.WriteLine("播放完成了!!!!"); MessageBox.Show("播放完成了!!!"); } } }
7.反射
a.理解程序集:包含资源文件,类型元数据,exe,dll.
程序集可以看做是一堆相关类的打一个包,相当于java中的jar包。
b.反射:动态获取一个程序集中的元数据的功能!
c.AssemblyInfo.cs程序集相关信息
d.程序集的好处(扩展性)
e.如何添加程序集的引用
1.添加引用;2.添加命名空间;3.Public关键字
f.反射!!!
Type类,通过它可以获取类中的所有信息包括方法,属性;可以动态调用类的属性,方法;Type是对类的描述。
反射就是通过.dll来创建对象,调用成员。
g.步骤
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Reflection;//1.反射命名空间 namespace Reflection { class Program { static void Main(string[] args) { //2.加载程序集文件,绝对路径 string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Common.dll"); Assembly ass = Assembly.LoadFile(path); //3.Assembly类 的方法GetExportedTypes,GetType,GetTypes //Type[] types = ass.GetExportedTypes(); //foreach (Type item in types) //{ // Console.WriteLine(item.FullName); //} //Type t = ass.GetType("Common.Person"); //4.动态创建对象 //调用了person类中默认无参数的构造函数! /* object o = ass.CreateInstance("Common.Person"); Console.WriteLine(o.GetType()); * */ //如果类中没有默认无参数的构造函数! Type t = ass.GetType("Common.Person"); object o = Activator.CreateInstance(t, "张三", 18); Console.WriteLine(o.GetType()); PropertyInfo[] pros = o.GetType().GetProperties(); foreach (PropertyInfo item in pros) { Console.WriteLine(item.Name); } //调用Person类中的SayHello MethodInfo mdi = o.GetType().GetMethod("SayHello"); mdi.Invoke(o, null); Console.ReadKey(); } } }
程序集中的相关类:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace Common { public class Person { public void Write() { File.WriteAllText("1.txt", "张三李四王五赵六天气"); } public Person(string name, int age) { this.Name = name; this.Age = age; } public int Age { get; set; } public string Name { get; set; } public void SayHello() { Console.WriteLine("我是Person类中的函数"); } } }
8.反射常用的函数
代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Reflection; namespace Reflection常用函数 { class Program { static void Main(string[] args) { string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Common.dll"); Assembly ass = Assembly.LoadFile(path); Type t_person = ass.GetType("Common.Person"); Type t_stu = ass.GetType("Common.Student"); Type t_tea = ass.GetType("Common.Teacher"); //Common.Person对象 object o_person = Activator.CreateInstance(t_person, "xx", 23); PropertyInfo pro =o_person.GetType().GetProperty("Age"); //Console.WriteLine(pro.GetValue(o_person));//获取属性值 //bool t = t_person.GetType().IsAssignableFrom(t_stu.GetType()); //bool t = o_person.GetType().IsAssignableFrom(t_person.GetType());//里氏转换,判断是否父类子类 //bool t = t_tea.GetType().IsInstanceOfType(o_person); //判断指定的对象是否是当前类的实例 bool t = t_stu.GetType().IsSubclassOf(t_person.GetType()); //判断当前类是否是指定类的子类 Console.WriteLine(t); Console.ReadKey(); } } }
You are never too old to set another goal or to dream a new dream!!!