From 926583f2fc146d7577a58e95b0665a11e8e7b8bc Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Mon, 20 Mar 2023 20:51:45 +0800 Subject: [PATCH] Update dynamic builder --- .../DynamicBuilder/DynamicProperyBuilder.cs | 30 ++++++++++ .../Abstract/DynamicBuilder/Helper.cs | 4 +- .../Abstract/DynamicBuilder/Master.cs | 57 ++++++++++++------- Src/Asp.Net/SqlSugar/SqlSugar.csproj | 1 + 4 files changed, 71 insertions(+), 21 deletions(-) create mode 100644 Src/Asp.Net/SqlSugar/Abstract/DynamicBuilder/DynamicProperyBuilder.cs diff --git a/Src/Asp.Net/SqlSugar/Abstract/DynamicBuilder/DynamicProperyBuilder.cs b/Src/Asp.Net/SqlSugar/Abstract/DynamicBuilder/DynamicProperyBuilder.cs new file mode 100644 index 000000000..19f5c6032 --- /dev/null +++ b/Src/Asp.Net/SqlSugar/Abstract/DynamicBuilder/DynamicProperyBuilder.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Emit; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + + public class DynamicProperyBuilder + { + public DynamicBuilder baseBuilder; + public DynamicProperyBuilder CreateProperty(string propertyName, Type properyType, SugarColumn table) + { + PropertyMetadata addItem = new PropertyMetadata(); + addItem.Name = propertyName; + addItem.Type = properyType; + addItem.CustomAttributes = new List() { baseBuilder.GetProperty(table) }; + baseBuilder.propertyAttr.Add(addItem); + return this; + } + + public Type BuilderType() + { + return DynamicBuilderHelper.CreateDynamicClass(baseBuilder.entityName, baseBuilder.propertyAttr, TypeAttributes.Public, baseBuilder.entityAttr, baseBuilder.baseType, baseBuilder.interfaces); + } + } +} diff --git a/Src/Asp.Net/SqlSugar/Abstract/DynamicBuilder/Helper.cs b/Src/Asp.Net/SqlSugar/Abstract/DynamicBuilder/Helper.cs index c3f215c8f..e40dcca61 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DynamicBuilder/Helper.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DynamicBuilder/Helper.cs @@ -10,7 +10,7 @@ namespace SqlSugar { public partial class DynamicBuilder { - private CustomAttributeBuilder GetEntity(SugarTable sugarTable) + internal CustomAttributeBuilder GetEntity(SugarTable sugarTable) { Type attributeType = typeof(SugarTable); ConstructorInfo attributeCtor = attributeType.GetConstructor(new Type[] { typeof(string) }); @@ -29,7 +29,7 @@ namespace SqlSugar }); return attributeBuilder; } - private CustomAttributeBuilder GetProperty(SugarColumn sugarTable) + internal CustomAttributeBuilder GetProperty(SugarColumn sugarTable) { Type attributeType = typeof(SugarColumn); ConstructorInfo attributeCtor = attributeType.GetConstructor(new Type[] { }); diff --git a/Src/Asp.Net/SqlSugar/Abstract/DynamicBuilder/Master.cs b/Src/Asp.Net/SqlSugar/Abstract/DynamicBuilder/Master.cs index f5a158dfb..81be775b4 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DynamicBuilder/Master.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DynamicBuilder/Master.cs @@ -5,44 +5,63 @@ using System.Reflection.Emit; using System.Reflection; using System.Text; using System.Threading.Tasks; +using System.Security.AccessControl; namespace SqlSugar { public partial class DynamicBuilder { - List propertyAttr = new List(); - List entityAttr = new List(); - string entityName { get; set; } - Type baseType = null; - Type[] interfaces = null; - private SqlSugarProvider context; + internal List propertyAttr = new List(); + internal List entityAttr = new List(); + internal string entityName { get; set; } + internal Type baseType = null; + internal Type[] interfaces = null; + internal SqlSugarProvider context; public DynamicBuilder(SqlSugarProvider context) { this.context = context; } - public DynamicBuilder CreateClass(string entityName, SugarTable table, Type baseType = null, Type[] interfaces = null) + public DynamicProperyBuilder CreateClass(string entityName, SugarTable table, Type baseType = null, Type[] interfaces = null) { this.baseType = baseType; this.interfaces = interfaces; this.entityName = entityName; this.entityAttr = new List() { GetEntity(table) }; - return this; - } - public DynamicBuilder CreateProperty(string propertyName, Type properyType, SugarColumn table) - { - PropertyMetadata addItem = new PropertyMetadata(); - addItem.Name = propertyName; - addItem.Type = properyType; - addItem.CustomAttributes = new List() { GetProperty(table) }; - this.propertyAttr.Add(addItem); - return this; + return new DynamicProperyBuilder() { baseBuilder=this}; } - public Type BuilderType() + public object CreateObjectByType(Type type, Dictionary dict) { - return DynamicBuilderHelper.CreateDynamicClass(this.entityName, propertyAttr, TypeAttributes.Public, this.entityAttr, baseType, interfaces); + // 创建一个默认的空对象 + object obj = Activator.CreateInstance(type); + + // 遍历字典中的每个 key-value 对 + foreach (KeyValuePair pair in dict) + { + // 获取对象中的属性 + PropertyInfo propertyInfo = type.GetProperty(pair.Key); + + if (propertyInfo != null) + { + // 如果找到了该属性,则将其值设置为字典中对应的值 + propertyInfo.SetValue(obj, Convert.ChangeType(pair.Value, propertyInfo.PropertyType)); + } + } + + // 返回创建的对象 + return obj; + } + + public List CreateObjectByType(Type type, List> dictList) + { + List result = new List(); + foreach (var item in dictList) + { + result.Add(CreateObjectByType(type, item)); + } + return result; } } } diff --git a/Src/Asp.Net/SqlSugar/SqlSugar.csproj b/Src/Asp.Net/SqlSugar/SqlSugar.csproj index 740d6cc04..8cd978987 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugar.csproj +++ b/Src/Asp.Net/SqlSugar/SqlSugar.csproj @@ -88,6 +88,7 @@ +