C#反射 字符串转为实体类,并做为参数传入泛型方法中使用
2021-03-28 05:25
标签:sem arc one ble amd col param each 单元格 工作中有这样一个需求,有N张不同的报表,每张报表对应一个数据源,以前采用SQL统计方式 ,统计数据采用内存方式,首先在内在里定义了数据源对应实体。统计条件用lamdba表达式式实现,通过工具对单元格进行定义。在实现过程中针对每一张表来写取数显示是很Low的了,取数条件定义都是规则的,统计实现就是一段C#代码,但是要针对不同的数据源也就是不同的List 根据字符串获取实体类型 泛型方法定义 调用方法代码 下面是全部代码,具体功能实现略过 C#反射 字符串转为实体类,并做为参数传入泛型方法中使用 标签:sem arc one ble amd col param each 单元格 原文地址:https://www.cnblogs.com/DevMuYuer/p/9337555.html 1 private Type getEntity(string typeName)
2 {
3 var workPath = AppDomain.CurrentDomain.BaseDirectory;
4 string[] files = Directory.GetFiles(workPath, "XXX.Utils.dll", SearchOption.TopDirectoryOnly);
5 foreach (string file in files)
6 {
7 string ext = file.Substring(file.LastIndexOf("."));
8 if (ext != ".dll") continue;
9 try
10 {
11 Assembly asm = Assembly.LoadFile(file);
12 Type[] allTypes = asm.GetTypes();
13 foreach (Type t in allTypes)
14 {
15 if (t.IsSubclassOf(typeof(XXDataRecord)))
16 {
17 if (t.Name.ToUpper().IsSame(typeName.ToUpper()))
18 return t;
19 }
20 }
21 }
22 catch
23 {
24 return null;
25 }
26 }
27 return null;
28 }
1 public XXDataMemory BuildDataMemoryPattern
1 var obj = new GenerateDataHelper(_Param);
2 Type myGenericClassType = obj.GetType();
3 //MakeGenericMethod 设置泛型参数类型
4 MethodInfo mi = myGenericClassType.GetMethod("BuildDataMemoryPattern").MakeGenericMethod(typeEntity);
5 int count = 0;
6 //设置调用方法参数
7 object[] invokeArgs = new object[] { zb, id, count };
8 //调用
9 var dt = (DataTable)mi.Invoke(obj, invokeArgs);
10 //获取回返数据
11 recordCount = (int)invokeArgs[9];
1 public class UtilFetchHelper
2 {
3 //获取实体类型
4 private Type getEntity(string typeName)
5 {
6 var workPath = AppDomain.CurrentDomain.BaseDirectory;
7 string[] files = Directory.GetFiles(workPath, "XXX.Utils.dll", SearchOption.TopDirectoryOnly);
8 foreach (string file in files)
9 {
10 string ext = file.Substring(file.LastIndexOf("."));
11 if (ext != ".dll") continue;
12 try
13 {
14 Assembly asm = Assembly.LoadFile(file);
15 Type[] allTypes = asm.GetTypes();
16 foreach (Type t in allTypes)
17 {
18 if (t.IsSubclassOf(typeof(XXDataRecord)))
19 {
20 if (t.Name.ToUpper().IsSame(typeName.ToUpper()))
21 return t;
22 }
23 }
24 }
25 catch
26 {
27 return null;
28 }
29 }
30 return null;
31 }
32
33 public DataTable BuildDataMemoryPattern(XXParams zb, string id, out int recordCount)
34 {
35 //根据传入参数获取实体名称
36 var entityName = GetDataSourceEntityName(zb);
37 //获取实体类型
38 Type typeEntity = getEntity(entityName);
39 if (typeEntity == null)
40 return null;
41 var obj = new GenerateDataHelper(_Param);
42 Type myGenericClassType = obj.GetType();
43 MethodInfo mi = myGenericClassType.GetMethod("BuildDataMemoryPattern").MakeGenericMethod(typeEntity);
44 int count = 0;
45 object[] invokeArgs = new object[] { zb, id, count };
46 var dt = (DataTable)mi.Invoke(obj, invokeArgs);
47 //这里获取回返数据
48 recordCount = (int)invokeArgs[9];
49 return dt;
50 }
51 }
52
53 public class GenerateDataHelper
54 {
55 private List
文章标题:C#反射 字符串转为实体类,并做为参数传入泛型方法中使用
文章链接:http://soscw.com/index.php/essay/68905.html