c# this关键字用法
标签:event 创建 typeof sys closed 情况下 优化 索引 参考文献
先说一下,this 后面跟的类型,就是要拓展方法的类型。注意要写在静态类中的静态方法,不然有些情况下访问
1 ///
2 /// 扩展类 用于为原始类扩展方法
3 ///
4 public static class AM_Extends
5 {
6 ///
7 /// 为string类扩展了一个child方法,实现某功能
8 ///
9 ///
10 ///
11 public static void Child( this string str,string new_str)
12 {
13 object obj = str;
14 str=new_str;
15 }
16 }
定义扩展方法
1 private void Form1_Load(object sender, EventArgs e)
2 {
3 string st1 = "123";
4 string st2 = "";
5 string st3 = "";
6 st3 = st2.Child(st1);//st3的值为“123”
7 }
调用实例
1 public class Test
2 {
3 public Test()
4 {
5 Console.WriteLine("无参构造函数");
6 }
7 // this()对应无参构造方法Test()
8 // 先执行Test(),后执行Test(string text)
9 public Test(string text) : this()
10 {
11 Console.WriteLine(text);
12 Console.WriteLine("有参构造函数");
13 }
14 }
View Code
-
用法4 索引器(基于索引器封装EPList,用于优化大数据下频发的Linq查询引发的程序性能问题,通过索引从list集合中查询数据)
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Reflection;
5 using System.Text;
6
7 namespace MyDemo.Web
8 {
9 ///
10 /// EPList 支持为List创建索引
11 ///
12 /// 类型
13 public class EPList 14 {
15 #region 成员变量
16
17 ///
18 /// 索引
19 ///
20 private Liststring[]> m_Index = new Liststring[]>();
21
22 ///
23 /// 缓存数据
24 ///
25 private Dictionarystring, List> m_CachedData = new Dictionarystring, List>();
26
27 ///
28 /// List数据源
29 ///
30 private List m_ListData = new List();
31
32 ///
33 /// 通过索引值取数据
34 ///
35 /// 索引字段
36 /// 字段值
37 ///
38 public Listthis[string[] indexFields]
39 {
40 get
41 {
42 string key = string.Join(",", indexFields);
43 if (m_CachedData.ContainsKey(key)) return m_CachedData[key];
44 return new List();
45 }
46 }
47
48 #endregion
49
50 #region 公共方法
51
52 ///
53 /// 创建索引
54 ///
55 /// 索引字段
56 public void CreateIndex(string[] indexFields)
57 {
58 if (m_Index.Contains(indexFields)) return;
59 m_Index.Add(indexFields);
60 }
61
62 ///
63 /// 添加
64 ///
65 /// 记录
66 public void Add(T record)
67 {
68 m_ListData.Add(record);
69 m_Index.ForEach(indexFields =>
70 {
71 string key = getKey(record, indexFields);
72 if (m_CachedData.ContainsKey(key))
73 {
74 m_CachedData[key].Add(record);
75 }
76 else
77 {
78 List list = new List { record };
79 m_CachedData.Add(key, list);
80 }
81 });
82 }
83
84 #endregion
85
86 #region 私有方法
87
88 ///
89 /// 获取值
90 ///
91 /// 记录
92 /// 字段名
93 ///
94 private object getValue(T record, string fieldName)
95 {
96 Type type = typeof(T);
97 PropertyInfo propertyInfo = type.GetProperty(fieldName);
98 return propertyInfo.GetValue(record, null);
99 }
100
101 ///
102 /// 获取Key
103 ///
104 /// 记录
105 /// 索引字段
106 private string getKey(T record, string[] indexFields)
107 {
108 Liststring> values = new Liststring>();
109 foreach (var field in indexFields)
110 {
111 string value = Convert.ToString(getValue(record, field));
112 values.Add(field + ":" + value);
113 }
114 return string.Join(",", values);
115 }
116
117 ///
118 /// 获取Key
119 ///
120 /// 索引字段
121 /// 字段值
122 ///
123 private string getKey(string[] indexFields, object[] fieldValues)
124 {
125 if (indexFields.Length != fieldValues.Length) return string.Empty;
126 for (int i = 0; i )
127 {
128 fieldValues[i] = indexFields[i] + ":" + fieldValues[i];
129 }
130 string key = string.Join(",", fieldValues);
131 return key;
132 }
133
134 #endregion
135 }
136 }
创建EPList
1 private EPList GetEPListData()
2 {
3 EPList eplist = new EPList();
4 eplist.CreateIndex(new string[] { "ParentId" });
5 string sql = "select Id,ParentId,Code,Name from SysDepart";
6 SqlHelper.ExecuteReader(sql, null, (reader) =>
7 {
8 SysDepartInfo record = new SysDepartInfo();
9 record.Id = Convert.ToString(reader["Id"]);
10 record.ParentId = Convert.ToString(reader["ParentId"]);
11 record.Code = Convert.ToString(reader["Code"]);
12 record.Name = Convert.ToString(reader["Name"]);
13 eplist.Add(record);
14 });
15 return eplist;
16 }
给EPList创建索引,并添加数据
1 ///
2 /// 获取子节点
3 ///
4 ///
5 ///
6 private IEnumerable CreateChildren(EPList data, TreeInfo node)
7 {
8 string id = node == null ? "0" : node.id;
9 List childNodes = new List();
10 // ParentId字段上创建了索引,所以这里就可以通过索引值直接取出下一层子节点数据,避免Linq查询引发的效率问题
11 var indexValues = new string[] { "ParentId:" + id };
12 var childData = data[indexValues];
13 childData.ForEach(record =>
14 {
15 var childNode = new TreeInfo
16 {
17 id = record.Id,
18 text = record.Code + " " + record.Name
19 };
20 childNodes.Add(childNode);
21 childNode.children = CreateChildren(data, childNode);
22 });
23 return childNodes.OrderBy(record => record.text);
24 }
通过索引高效查询数据
参考文献: https://www.cnblogs.com/yellowcool/p/7908607.html
c# this关键字用法
标签:event 创建 typeof sys closed 情况下 优化 索引 参考文献
原文地址:https://www.cnblogs.com/MatureMan/p/12303347.html
评论