From 43a8e8bee9836ad5d12a6bcdc9696cd7eb0202fc Mon Sep 17 00:00:00 2001 From: d4ilys <963922242@qq.com> Date: Mon, 24 Apr 2023 14:28:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8A=A8=E6=80=81=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E5=AE=9E=E4=BD=93API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql/Extensions/CodeFirstExtensions.cs | 7 ++-- FreeSql/FreeSql.xml | 51 +++++++++++++++++++++++ FreeSql/Internal/DynamicCompileBuilder.cs | 33 ++++++++------- 3 files changed, 73 insertions(+), 18 deletions(-) diff --git a/FreeSql/Extensions/CodeFirstExtensions.cs b/FreeSql/Extensions/CodeFirstExtensions.cs index f70f3bd7..e7f14b58 100644 --- a/FreeSql/Extensions/CodeFirstExtensions.cs +++ b/FreeSql/Extensions/CodeFirstExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using FreeSql.DataAnnotations; using FreeSql.Internal; namespace FreeSql.Extensions @@ -14,9 +15,9 @@ namespace FreeSql.Extensions /// 动态创建Class Type /// /// - 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); } /// @@ -25,7 +26,7 @@ namespace FreeSql.Extensions /// /// /// - public static object CreateObjectByType(this ICodeFirst codeFirst, Type type, + public static object CreateDynamicEntityInstance(this Type type, Dictionary porpertys) { return DynamicCompileBuilder.CreateObjectByType(type, porpertys); diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index 5323d7da..fef3c125 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -1073,6 +1073,20 @@ + + + 动态创建Class Type + + + + + + 根据动态构建的Class生成实例并进行属性赋值 + + + + + 获取实体的主键值,以 "*|_,[,_|*" 分割,当任意一个主键属性无值时,返回 null @@ -4248,6 +4262,43 @@ Dict:key=属性名,value=注释 + + + 配置Class + + 类名 + 类标记的特性[Table(Name = "xxx")] + + + + + 配置属性 + + 属性名称 + 属性类型 + 属性标记的特性[Column(IsPrimary = true)] + + + + + Emit动态创建出Class - Type + + + + + + 首字母小写 + + + + + + + 首字母大写 + + + + 更新实体的元数据 diff --git a/FreeSql/Internal/DynamicCompileBuilder.cs b/FreeSql/Internal/DynamicCompileBuilder.cs index 16673161..99939f16 100644 --- a/FreeSql/Internal/DynamicCompileBuilder.cs +++ b/FreeSql/Internal/DynamicCompileBuilder.cs @@ -11,7 +11,7 @@ using System.Reflection.Emit; namespace FreeSql.Internal { #if net40 || NETSTANDARD2_0 - + #else public class DynamicCompileBuilder { @@ -37,15 +37,15 @@ namespace FreeSql.Internal /// /// 属性名称 /// 属性类型 - /// 属性标记的特性[Column(IsPrimary = true)] + /// 属性标记的特性[Column(IsPrimary = true)] /// - public DynamicCompileBuilder SetProperty(string propertyName, Type propertyType, ColumnAttribute columnAttribute) + public DynamicCompileBuilder Property(string propertyName, Type propertyType, params Attribute [] attributes) { _properties.Add(new DynamicPropertyInfo() { PropertyName = propertyName, - PropertyType = propertyType, - ColumnAttribute = columnAttribute + PropertyType = propertyType, + Attributes = attributes }); return this; } @@ -103,24 +103,27 @@ namespace FreeSql.Internal propertyBuilder.SetGetMethod(methodGet); propertyBuilder.SetSetMethod(methodSet); - //设置特性 - SetColumnAttribute(ref propertyBuilder, pinfo?.ColumnAttribute); + foreach (var pinfoAttribute in pinfo.Attributes) + { + //设置特性 + SetPropertyAttribute(ref propertyBuilder, pinfoAttribute); + } } } - private void SetColumnAttribute(ref PropertyBuilder propertyBuilder, ColumnAttribute columnAttribute = null) + private void SetPropertyAttribute(ref PropertyBuilder propertyBuilder, T tAttribute) { - if (columnAttribute == null) + if (tAttribute == null) return; 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 constructor = typeof(ColumnAttribute).GetConstructor(new Type[] { }); + var propertyInfos = tAttribute.GetType().GetProperties().Where(p => p.CanWrite == true).ToArray(); + var constructor = tAttribute.GetType().GetConstructor(new Type[] { }); var customAttributeBuilder = new CustomAttributeBuilder(constructor, new object[0], propertyInfos, propertyValues.ToArray()); propertyBuilder.SetCustomAttribute(customAttributeBuilder); @@ -226,8 +229,8 @@ namespace FreeSql.Internal internal class DynamicPropertyInfo { public string PropertyName { get; set; } = string.Empty; - public Type PropertyType { get; set; } = null; - public ColumnAttribute ColumnAttribute { get; set; } = null; + public Type PropertyType { get; set; } + public Attribute [] Attributes { get; set; } } }