增加动态创建实体API

This commit is contained in:
d4ilys 2023-04-24 14:28:45 +08:00
parent 91da92b11f
commit 43a8e8bee9
3 changed files with 73 additions and 18 deletions

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using FreeSql.DataAnnotations;
using FreeSql.Internal; using FreeSql.Internal;
namespace FreeSql.Extensions namespace FreeSql.Extensions
@ -14,9 +15,9 @@ namespace FreeSql.Extensions
/// 动态创建Class Type /// 动态创建Class Type
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static DynamicCompileBuilder DynamicBuilder(this ICodeFirst codeFirst) public static DynamicCompileBuilder DynamicEntity(this ICodeFirst codeFirst, string className, TableAttribute tableAttribute)
{ {
return new DynamicCompileBuilder(); return new DynamicCompileBuilder().SetClass(className, tableAttribute);
} }
/// <summary> /// <summary>
@ -25,7 +26,7 @@ namespace FreeSql.Extensions
/// <param name="type"></param> /// <param name="type"></param>
/// <param name="porpertys"></param> /// <param name="porpertys"></param>
/// <returns></returns> /// <returns></returns>
public static object CreateObjectByType(this ICodeFirst codeFirst, Type type, public static object CreateDynamicEntityInstance(this Type type,
Dictionary<string, object> porpertys) Dictionary<string, object> porpertys)
{ {
return DynamicCompileBuilder.CreateObjectByType(type, porpertys); return DynamicCompileBuilder.CreateObjectByType(type, porpertys);

View File

@ -1073,6 +1073,20 @@
</summary> </summary>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:FreeSql.Extensions.CodeFirstExtensions.DynamicEntity(FreeSql.ICodeFirst,System.String,FreeSql.DataAnnotations.TableAttribute)">
<summary>
动态创建Class Type
</summary>
<returns></returns>
</member>
<member name="M:FreeSql.Extensions.CodeFirstExtensions.CreateObjectByType(System.Type,System.Collections.Generic.Dictionary{System.String,System.Object})">
<summary>
根据动态构建的Class生成实例并进行属性赋值
</summary>
<param name="type"></param>
<param name="porpertys"></param>
<returns></returns>
</member>
<member name="M:FreeSql.Extensions.EntityUtil.EntityUtilExtensions.GetEntityKeyString(IFreeSql,System.Type,System.Object,System.Boolean,System.String)"> <member name="M:FreeSql.Extensions.EntityUtil.EntityUtilExtensions.GetEntityKeyString(IFreeSql,System.Type,System.Object,System.Boolean,System.String)">
<summary> <summary>
获取实体的主键值,以 "*|_,[,_|*" 分割,当任意一个主键属性无值时,返回 null 获取实体的主键值,以 "*|_,[,_|*" 分割,当任意一个主键属性无值时,返回 null
@ -4248,6 +4262,43 @@
<param name="type"></param> <param name="type"></param>
<returns>Dictkey=属性名value=注释</returns> <returns>Dictkey=属性名value=注释</returns>
</member> </member>
<member name="M:FreeSql.Internal.DynamicCompileBuilder.SetClass(System.String,FreeSql.DataAnnotations.TableAttribute)">
<summary>
配置Class
</summary>
<param name="className">类名</param>
<param name="tableAttribute">类标记的特性[Table(Name = "xxx")]</param>
<returns></returns>
</member>
<member name="M:FreeSql.Internal.DynamicCompileBuilder.Property(System.String,System.Type,System.Attribute[])">
<summary>
配置属性
</summary>
<param name="propertyName">属性名称</param>
<param name="propertyType">属性类型</param>
<param name="attributes">属性标记的特性[Column(IsPrimary = true)]</param>
<returns></returns>
</member>
<member name="M:FreeSql.Internal.DynamicCompileBuilder.Build">
<summary>
Emit动态创建出Class - Type
</summary>
<returns></returns>
</member>
<member name="M:FreeSql.Internal.DynamicCompileBuilder.FirstCharToLower(System.String)">
<summary>
首字母小写
</summary>
<param name="input"></param>
<returns></returns>
</member>
<member name="M:FreeSql.Internal.DynamicCompileBuilder.FirstCharToUpper(System.String)">
<summary>
首字母大写
</summary>
<param name="input"></param>
<returns></returns>
</member>
<member name="P:FreeSql.Internal.DbUpdateVersionException.Table"> <member name="P:FreeSql.Internal.DbUpdateVersionException.Table">
<summary> <summary>
更新实体的元数据 更新实体的元数据

View File

@ -11,7 +11,7 @@ using System.Reflection.Emit;
namespace FreeSql.Internal namespace FreeSql.Internal
{ {
#if net40 || NETSTANDARD2_0 #if net40 || NETSTANDARD2_0
#else #else
public class DynamicCompileBuilder public class DynamicCompileBuilder
{ {
@ -37,15 +37,15 @@ namespace FreeSql.Internal
/// </summary> /// </summary>
/// <param name="propertyName">属性名称</param> /// <param name="propertyName">属性名称</param>
/// <param name="propertyType">属性类型</param> /// <param name="propertyType">属性类型</param>
/// <param name="columnAttribute">属性标记的特性[Column(IsPrimary = true)]</param> /// <param name="attributes">属性标记的特性[Column(IsPrimary = true)]</param>
/// <returns></returns> /// <returns></returns>
public DynamicCompileBuilder SetProperty(string propertyName, Type propertyType, ColumnAttribute columnAttribute) public DynamicCompileBuilder Property(string propertyName, Type propertyType, params Attribute [] attributes)
{ {
_properties.Add(new DynamicPropertyInfo() _properties.Add(new DynamicPropertyInfo()
{ {
PropertyName = propertyName, PropertyName = propertyName,
PropertyType = propertyType, PropertyType = propertyType,
ColumnAttribute = columnAttribute Attributes = attributes
}); });
return this; return this;
} }
@ -103,24 +103,27 @@ namespace FreeSql.Internal
propertyBuilder.SetGetMethod(methodGet); propertyBuilder.SetGetMethod(methodGet);
propertyBuilder.SetSetMethod(methodSet); propertyBuilder.SetSetMethod(methodSet);
//设置特性 foreach (var pinfoAttribute in pinfo.Attributes)
SetColumnAttribute(ref propertyBuilder, pinfo?.ColumnAttribute); {
//设置特性
SetPropertyAttribute(ref propertyBuilder, pinfoAttribute);
}
} }
} }
private void SetColumnAttribute(ref PropertyBuilder propertyBuilder, ColumnAttribute columnAttribute = null) private void SetPropertyAttribute<T>(ref PropertyBuilder propertyBuilder, T tAttribute)
{ {
if (columnAttribute == null) if (tAttribute == null)
return; return;
var propertyValues = new ArrayList(); var propertyValues = new ArrayList();
foreach (var propertyInfo in columnAttribute.GetType().GetProperties().Where(p => p.CanWrite == true)) foreach (var propertyInfo in tAttribute.GetType().GetProperties().Where(p => p.CanWrite == true))
{ {
propertyValues.Add(propertyInfo.GetValue(columnAttribute)); propertyValues.Add(propertyInfo.GetValue(tAttribute));
} }
var propertyInfos = typeof(ColumnAttribute).GetProperties().Where(p => p.CanWrite == true).ToArray(); var propertyInfos = tAttribute.GetType().GetProperties().Where(p => p.CanWrite == true).ToArray();
var constructor = typeof(ColumnAttribute).GetConstructor(new Type[] { }); var constructor = tAttribute.GetType().GetConstructor(new Type[] { });
var customAttributeBuilder = var customAttributeBuilder =
new CustomAttributeBuilder(constructor, new object[0], propertyInfos, propertyValues.ToArray()); new CustomAttributeBuilder(constructor, new object[0], propertyInfos, propertyValues.ToArray());
propertyBuilder.SetCustomAttribute(customAttributeBuilder); propertyBuilder.SetCustomAttribute(customAttributeBuilder);
@ -226,8 +229,8 @@ namespace FreeSql.Internal
internal class DynamicPropertyInfo internal class DynamicPropertyInfo
{ {
public string PropertyName { get; set; } = string.Empty; public string PropertyName { get; set; } = string.Empty;
public Type PropertyType { get; set; } = null; public Type PropertyType { get; set; }
public ColumnAttribute ColumnAttribute { get; set; } = null; public Attribute [] Attributes { get; set; }
} }
} }