mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-24 18:04:52 +08:00
Update dynamic builder
This commit is contained in:
parent
f8ea52a9f0
commit
926583f2fc
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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[] { });
|
||||
|
@ -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;
|
||||
}
|
||||
public DynamicBuilder CreateProperty(string propertyName, Type properyType, SugarColumn table)
|
||||
{
|
||||
PropertyMetadata addItem = new PropertyMetadata();
|
||||
addItem.Name = propertyName;
|
||||
addItem.Type = properyType;
|
||||
addItem.CustomAttributes = new List<CustomAttributeBuilder>() { GetProperty(table) };
|
||||
this.propertyAttr.Add(addItem);
|
||||
return this;
|
||||
return new DynamicProperyBuilder() { baseBuilder=this};
|
||||
}
|
||||
|
||||
public Type BuilderType()
|
||||
public object CreateObjectByType(Type type, Dictionary<string, object> dict)
|
||||
{
|
||||
return DynamicBuilderHelper.CreateDynamicClass(this.entityName, propertyAttr, TypeAttributes.Public, this.entityAttr, baseType, interfaces);
|
||||
// 创建一个默认的空对象
|
||||
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 List<object> CreateObjectByType(Type type, List<Dictionary<string, object>> dictList)
|
||||
{
|
||||
List<object> result = new List<object>();
|
||||
foreach (var item in dictList)
|
||||
{
|
||||
result.Add(CreateObjectByType(type, item));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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…
Reference in New Issue
Block a user