標籤:意義 訪問 cut ret var 使用 help isa pre
委託的聲明
public delegate void NoReturnNoPara();
存取修飾詞 delegate關鍵字 傳回值 委託名
委託的調用
NoReturnNoPara nrnp = new NoReturnNoPara(傳遞方法名);
傳遞的方法必須滿足,沒有傳回值,沒有參數的限制。執行個體方法,靜態方法,虛方法
nrnp.Invoke();
泛型委派
Func:接收0-17個輸入參數,一個返回參數的泛型委派
Action:接收0-17個輸入參數,0個返回參數的泛型委派
委託的意義:解耦
使用泛型+委託實現 ADO.NET的增刪改查,將公用實現部分抽取出來,採用泛型委派的方式將增/刪/改/查邏輯分離出去。
做到邏輯的解耦。實現代碼
1 public class Company2 {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;
事件只能外部訂閱,內部調用,事件只能出現在+=,-=組合符號的左邊。
4.net基礎之委託事件