您好,登錄后才能下訂單哦!
用法1 為原始類型擴展方法
先說一下,this 后面跟的類型,就是要拓展方法的類型。注意要寫在靜態類中的靜態方法,不然有些情況下訪問
/// <summary> /// 擴展類 用于為原始類擴展方法 /// </summary> public static class AM_Extends { /// <summary> /// 為string類擴展了一個child方法,實現某功能 /// </summary> /// <param name="str"></param> /// <param name="new_str"></param> public static void Child( this string str,string new_str) { object obj = str; str=new_str; } } 定義擴展方法
private void Form1_Load(object sender, EventArgs e) { string st1 = "123"; string st2 = ""; string st3 = ""; st3 = st2.Child(st1);//st3的值為“123” } 調用實例
用法2 this代表當前類的實例對象
用法3 用this串聯構造函數
public class Test { public Test() { Console.WriteLine("無參構造函數"); } // this()對應無參構造方法Test() // 先執行Test(),后執行Test(string text) public Test(string text) : this() { Console.WriteLine(text); Console.WriteLine("有參構造函數"); } }
用法4 索引器(基于索引器封裝EPList,用于優化大數據下頻發的Linq查詢引發的程序性能問題,通過索引從list集合中查詢數據)
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; namespace MyDemo.Web { /// <summary> /// EPList 支持為List創建索引 /// </summary> /// <typeparam name="T">類型</typeparam> public class EPList<T> { #region 成員變量 /// <summary> /// 索引 /// </summary> private List<string[]> m_Index = new List<string[]>(); /// <summary> /// 緩存數據 /// </summary> private Dictionary<string, List<T>> m_CachedData = new Dictionary<string, List<T>>(); /// <summary> /// List數據源 /// </summary> private List<T> m_ListData = new List<T>(); /// <summary> /// 通過索引值取數據 /// </summary> /// <param name="indexFields">索引字段</param> /// <param name="fieldValues">字段值</param> /// <returns></returns> public List<T> this[string[] indexFields] { get { string key = string.Join(",", indexFields); if (m_CachedData.ContainsKey(key)) return m_CachedData[key]; return new List<T>(); } } #endregion #region 公共方法 /// <summary> /// 創建索引 /// </summary> /// <param name="indexFields">索引字段</param> public void CreateIndex(string[] indexFields) { if (m_Index.Contains(indexFields)) return; m_Index.Add(indexFields); } /// <summary> /// 添加 /// </summary> /// <param name="record">記錄</param> public void Add(T record) { m_ListData.Add(record); m_Index.ForEach(indexFields => { string key = getKey(record, indexFields); if (m_CachedData.ContainsKey(key)) { m_CachedData[key].Add(record); } else { List<T> list = new List<T> { record }; m_CachedData.Add(key, list); } }); } #endregion #region 私有方法 /// <summary> /// 獲取值 /// </summary> /// <param name="record">記錄</param> /// <param name="fieldName">字段名</param> /// <returns></returns> private object getValue(T record, string fieldName) { Type type = typeof(T); PropertyInfo propertyInfo = type.GetProperty(fieldName); return propertyInfo.GetValue(record, null); } /// <summary> /// 獲取Key /// </summary> /// <param name="record">記錄</param> /// <param name="indexFields">索引字段</param> private string getKey(T record, string[] indexFields) { List<string> values = new List<string>(); foreach (var field in indexFields) { string value = Convert.ToString(getValue(record, field)); values.Add(field + ":" + value); } return string.Join(",", values); } /// <summary> /// 獲取Key /// </summary> /// <param name="indexFields">索引字段</param> /// <param name="fieldValues">字段值</param> /// <returns></returns> private string getKey(string[] indexFields, object[] fieldValues) { if (indexFields.Length != fieldValues.Length) return string.Empty; for (int i = 0; i < indexFields.Length; i++) { fieldValues[i] = indexFields[i] + ":" + fieldValues[i]; } string key = string.Join(",", fieldValues); return key; } #endregion } } 創建EPList
給EPList創建索引,并添加數據
private EPList<SysDepartInfo> GetEPListData() { EPList<SysDepartInfo> eplist = new EPList<SysDepartInfo>(); eplist.CreateIndex(new string[] { "ParentId" }); string sql = "select Id,ParentId,Code,Name from SysDepart"; SqlHelper.ExecuteReader(sql, null, (reader) => { SysDepartInfo record = new SysDepartInfo(); record.Id = Convert.ToString(reader["Id"]); record.ParentId = Convert.ToString(reader["ParentId"]); record.Code = Convert.ToString(reader["Code"]); record.Name = Convert.ToString(reader["Name"]); eplist.Add(record); }); return eplist; } 給EPList創建索引,并添加數據
private EPList<SysDepartInfo> GetEPListData() { EPList<SysDepartInfo> eplist = new EPList<SysDepartInfo>(); eplist.CreateIndex(new string[] { "ParentId" }); string sql = "select Id,ParentId,Code,Name from SysDepart"; SqlHelper.ExecuteReader(sql, null, (reader) => { SysDepartInfo record = new SysDepartInfo(); record.Id = Convert.ToString(reader["Id"]); record.ParentId = Convert.ToString(reader["ParentId"]); record.Code = Convert.ToString(reader["Code"]); record.Name = Convert.ToString(reader["Name"]); eplist.Add(record); }); return eplist; }
給EPList創建索引,并添加數據
/// <summary> /// 獲取子節點 /// </summary> /// <param name="data"></param> /// <param name="parentId"></param> private IEnumerable<TreeInfo> CreateChildren(EPList<SysDepartInfo> data, TreeInfo node) { string id = node == null ? "0" : node.id; List<TreeInfo> childNodes = new List<TreeInfo>(); // ParentId字段上創建了索引,所以這里就可以通過索引值直接取出下一層子節點數據,避免Linq查詢引發的效率問題 var indexValues = new string[] { "ParentId:" + id }; var childData = data[indexValues]; childData.ForEach(record => { var childNode = new TreeInfo { id = record.Id, text = record.Code + " " + record.Name }; childNodes.Add(childNode); childNode.children = CreateChildren(data, childNode); }); return childNodes.OrderBy(record => record.text); } 通過索引高效查詢數據
以上就是小編整理的全部相關知識點內容,感謝大家的學習和對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。