Modify dynamic class building

This commit is contained in:
sunkaixuan
2023-04-08 13:13:08 +08:00
parent 39a70306d9
commit 31b4922411
4 changed files with 31 additions and 3 deletions

View File

@@ -18,7 +18,7 @@ namespace SqlSugar
return new DynamicProperyBuilder();
}
public DynamicBuilder baseBuilder;
public DynamicProperyBuilder CreateProperty(string propertyName, Type properyType, SugarColumn column=null)
public DynamicProperyBuilder CreateProperty(string propertyName, Type properyType, SugarColumn column=null,bool isSplitField=false)
{
if (column == null)
{
@@ -32,6 +32,10 @@ namespace SqlSugar
addItem.Type = properyType;
addItem.CustomAttributes = new List<CustomAttributeBuilder>() { baseBuilder.GetProperty(column) };
baseBuilder.propertyAttr.Add(addItem);
if (isSplitField)
{
addItem.CustomAttributes.Add(baseBuilder.GetSplitFieldAttr(new SplitFieldAttribute()));
}
return this;
}
public DynamicProperyBuilder WithCache(bool isCache=true)

View File

@@ -10,6 +10,26 @@ namespace SqlSugar
{
public partial class DynamicBuilder
{
internal CustomAttributeBuilder GetSplitEntityAttr(SplitTableAttribute sugarTable)
{
Type attributeType = typeof(SplitTableAttribute);
ConstructorInfo attributeCtor = attributeType.GetConstructor(new Type[] { typeof(SplitType) });
CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(attributeCtor, new object[] { sugarTable.SplitType },
new PropertyInfo[] {
attributeType.GetProperty(nameof(SplitTableAttribute.SplitType)),
}
, new object[] {
sugarTable.SplitType
});
return attributeBuilder;
}
internal CustomAttributeBuilder GetSplitFieldAttr(SplitFieldAttribute fieldAttribute)
{
Type attributeType = typeof(SplitFieldAttribute);
ConstructorInfo attributeCtor = attributeType.GetConstructor(new Type[] { });
CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(attributeCtor, new object[] { });
return attributeBuilder;
}
internal CustomAttributeBuilder GetEntity(SugarTable sugarTable)
{
Type attributeType = typeof(SugarTable);

View File

@@ -23,7 +23,7 @@ namespace SqlSugar
this.context = context;
}
public DynamicProperyBuilder CreateClass(string entityName, SugarTable table=null, Type baseType = null, Type[] interfaces = null)
public DynamicProperyBuilder CreateClass(string entityName, SugarTable table=null, Type baseType = null, Type[] interfaces = null,SplitTableAttribute splitTableAttribute=null)
{
this.baseType = baseType;
this.interfaces = interfaces;
@@ -33,6 +33,10 @@ namespace SqlSugar
table = new SugarTable() { TableName = entityName };
}
this.entityAttr = new List<CustomAttributeBuilder>() { GetEntity(table) };
if (splitTableAttribute != null)
{
this.entityAttr.Add(GetSplitEntityAttr(splitTableAttribute));
}
return new DynamicProperyBuilder() { baseBuilder=this};
}

View File

@@ -11,6 +11,6 @@ namespace SqlSugar
{
public string Name { get; set; }
public Type Type { get; set; }
public IEnumerable<CustomAttributeBuilder> CustomAttributes { get; set; }
public List<CustomAttributeBuilder> CustomAttributes { get; set; }
}
}