委托的声明
public delegate void NoReturnNoPara();
访问修饰符 delegate关键字 返回值 委托名
委托的调用
NoReturnNoPara nrnp = new NoReturnNoPara(传递方法名);
传递的方法必须满足,没有返回值,没有参数的限制。实例方法,静态方法,虚方法
nrnp.Invoke();
泛型委托
Func:接收0-17个输入参数,一个返回参数的泛型委托
Action:接收0-17个输入参数,0个返回参数的泛型委托
委托的意义:解耦
使用泛型+委托实现 ADO.NET的增删改查,将公共实现部分抽取出来,采用泛型委托的方式将增/删/改/查逻辑分离出去。
做到逻辑的解耦。实现代码
1 public class Company 2 { 3 public int Id { get; set; } 4 public string Name { get; set; } 5 public DateTime CreateTime { get; set; } 6 public int CreatorId { get; set; } 7 public int? LastModifierId { get; set; } 8 public DateTime? LastModifyTime { get; set; } 9 }
Company类
1 public class DBHelper<T> where T:class,new() 2 { 3 private static string connectionStr = ConfigurationManager.ConnectionStrings["connectionStr"].ConnectionString; 4 5 private W ExecuteSql<W>(string sql,Func<IDbCommand,W> func) { 6 using (SqlConnection conn = new SqlConnection(connectionStr)) 7 { 8 IDbCommand command = conn.CreateCommand(); 9 conn.Open(); 10 return func(command); 11 } 12 } 13 public bool Add(T t) 14 { 15 Type type = typeof(T); 16 var c = string.Join(",", type.GetProperties().Where(p => p.Name != "Id").Select(p => string.Format("[{0}]", p.Name))); 17 var v = string.Join(",", type.GetProperties().Where(p => p.Name != "Id").Select(p => string.Format("@{0}", p.Name))); 18 var list = new List<SqlParameter>(); 19 foreach (var item in type.GetProperties().Where(p => p.Name != "Id")) 20 { 21 list.Add(new SqlParameter("@" + item.Name, item.GetValue(t))); 22 } 23 string sql = string.Format("insert into {0} ({1}) values ({2});", type.Name, c, v); 24 25 return ExecuteSql<bool>(sql, cmd => 26 { 27 foreach (var item in list) 28 { 29 cmd.Parameters.Add(item); 30 } 31 cmd.CommandText = sql; 32 cmd.CommandType = CommandType.Text; 33 bool isAdded = cmd.ExecuteNonQuery() > 0; 34 cmd.Connection.Close(); 35 return isAdded; 36 }); 37 } 38 39 public bool Delete(int id) 40 { 41 string sql = "DELETE FROM [dbo].[Company] where [email protected]"; 42 return ExecuteSql<bool>(sql, cmd => 43 { 44 cmd.Parameters.Add(new SqlParameter("@Id", id)); 45 cmd.CommandText = sql; 46 cmd.CommandType = CommandType.Text; 47 bool isDeleted = cmd.ExecuteNonQuery()>0; 48 cmd.Connection.Close(); 49 return isDeleted; 50 }); 51 } 52 53 public bool Update(T t) { 54 Type type = typeof(T); 55 var c = string.Join(",", type.GetProperties().Where(p => p.Name != "Id").Select(p => string.Format("[{0}][email protected]{1}", p.Name,p.Name))); 56 var list = new List<SqlParameter>(); 57 58 foreach (var item in type.GetProperties()) 59 { 60 list.Add(new SqlParameter("@" + item.Name,item.GetValue(t))); 61 } 62 string sql = string.Format("UPDATE [dbo].[Company] SET {0} where Id = @Id",c); 63 return ExecuteSql<bool>(sql, cmd => 64 { 65 foreach (var item in list) 66 { 67 cmd.Parameters.Add(item); 68 } 69 cmd.CommandText = sql; 70 cmd.CommandType = CommandType.Text; 71 bool isUpdated = cmd.ExecuteNonQuery() > 0; 72 cmd.Connection.Close(); 73 return isUpdated; 74 }); 75 } 76 public IList<T> Query() { 77 return null; 78 } 79 80 public IList<T> Query(string where) { 81 return null; 82 } 83 }
使用委托解耦分离逻辑实现ADO.NET_SqlHelper
委托的意义:异步多线程:
委托的意义:多播委托
delegate:System.MulticastDelegate
构成一个委托链条,移除委托中的某一项是从委托链的尾部去查找完全匹配的方法进行移除
多播委托的应用场景:猫叫引发的一系列事件
委托事件:观察者模式
public event Action action;
事件只能外部订阅,内部调用,事件只能出现在+=,-=组合符号的左边。
时间: 2024-10-23 15:57:56