Browse Source

Update dynamic builder

pull/31/head
sunkaixuan 2 years ago
parent
commit
926583f2fc
  1. 30
      Src/Asp.Net/SqlSugar/Abstract/DynamicBuilder/DynamicProperyBuilder.cs
  2. 4
      Src/Asp.Net/SqlSugar/Abstract/DynamicBuilder/Helper.cs
  3. 53
      Src/Asp.Net/SqlSugar/Abstract/DynamicBuilder/Master.cs
  4. 1
      Src/Asp.Net/SqlSugar/SqlSugar.csproj

30
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<CustomAttributeBuilder>() { 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);
}
}
}

4
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[] { });

53
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<PropertyMetadata> propertyAttr = new List<PropertyMetadata>();
List<CustomAttributeBuilder> entityAttr = new List<CustomAttributeBuilder>();
string entityName { get; set; }
Type baseType = null;
Type[] interfaces = null;
private SqlSugarProvider context;
internal List<PropertyMetadata> propertyAttr = new List<PropertyMetadata>();
internal List<CustomAttributeBuilder> entityAttr = new List<CustomAttributeBuilder>();
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<CustomAttributeBuilder>() { GetEntity(table) };
return this;
return new DynamicProperyBuilder() { baseBuilder=this};
}
public DynamicBuilder CreateProperty(string propertyName, Type properyType, SugarColumn table)
public object CreateObjectByType(Type type, Dictionary<string, object> dict)
{
PropertyMetadata addItem = new PropertyMetadata();
addItem.Name = propertyName;
addItem.Type = properyType;
addItem.CustomAttributes = new List<CustomAttributeBuilder>() { GetProperty(table) };
this.propertyAttr.Add(addItem);
return this;
// 创建一个默认的空对象
object obj = Activator.CreateInstance(type);
// 遍历字典中的每个 key-value 对
foreach (KeyValuePair<string, object> pair in dict)
{
// 获取对象中的属性
PropertyInfo propertyInfo = type.GetProperty(pair.Key);
if (propertyInfo != null)
{
// 如果找到了该属性,则将其值设置为字典中对应的值
propertyInfo.SetValue(obj, Convert.ChangeType(pair.Value, propertyInfo.PropertyType));
}
}
// 返回创建的对象
return obj;
}
public Type BuilderType()
public List<object> CreateObjectByType(Type type, List<Dictionary<string, object>> dictList)
{
return DynamicBuilderHelper.CreateDynamicClass(this.entityName, propertyAttr, TypeAttributes.Public, this.entityAttr, baseType, interfaces);
List<object> result = new List<object>();
foreach (var item in dictList)
{
result.Add(CreateObjectByType(type, item));
}
return result;
}
}
}

1
Src/Asp.Net/SqlSugar/SqlSugar.csproj

@ -88,6 +88,7 @@
<Compile Include="Abstract\DeleteProvider\LogicDeleteProvider.cs" />
<Compile Include="Abstract\DeleteProvider\SplitTableDeleteByObjectProvider.cs" />
<Compile Include="Abstract\DeleteProvider\SplitTableDeleteProvider.cs" />
<Compile Include="Abstract\DynamicBuilder\DynamicProperyBuilder.cs" />
<Compile Include="Abstract\DynamicBuilder\Helper.cs" />
<Compile Include="Abstract\DynamicBuilder\EmitTool.cs" />
<Compile Include="Abstract\DynamicBuilder\Master.cs" />

Loading…
Cancel
Save