From 235a2c26363025b5ee481e10c7b910f026df3966 Mon Sep 17 00:00:00 2001
From: 2881099 <2881099@qq.com>
Date: Thu, 4 May 2023 21:46:11 +0800
Subject: [PATCH] =?UTF-8?q?-=20=E5=8A=A8=E6=80=81=E6=93=8D=E4=BD=9C?=
=?UTF-8?q?=E8=A1=A8=E7=BB=93=E6=9E=84=E7=9B=B8=E5=85=B3=E7=9A=84=20API?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FreeSql/Extensions/CodeFirstExtensions.cs | 26 --------
FreeSql/Extensions/FreeSqlGlobalExtensions.cs | 15 +++++
FreeSql/Extensions/TypeExtensions.cs | 40 ------------
FreeSql/Internal/DynamicCompileBuilder.cs | 64 +++++++++----------
4 files changed, 44 insertions(+), 101 deletions(-)
delete mode 100644 FreeSql/Extensions/CodeFirstExtensions.cs
delete mode 100644 FreeSql/Extensions/TypeExtensions.cs
diff --git a/FreeSql/Extensions/CodeFirstExtensions.cs b/FreeSql/Extensions/CodeFirstExtensions.cs
deleted file mode 100644
index 818be04c..00000000
--- a/FreeSql/Extensions/CodeFirstExtensions.cs
+++ /dev/null
@@ -1,26 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Reflection;
-using System.Text;
-using FreeSql.DataAnnotations;
-using FreeSql.Internal;
-
-namespace FreeSql.Extensions
-{
-#if net40 || NETSTANDARD2_0
-#else
- public static class CodeFirstExtensions
- {
- ///
- /// 动态构建Class Type
- ///
- ///
- public static DynamicCompileBuilder DynamicEntity(this ICodeFirst codeFirst, string className,
- TableAttribute tableAttribute)
- {
- return new DynamicCompileBuilder().SetClass(className, tableAttribute);
- }
-
- }
-#endif
-}
\ No newline at end of file
diff --git a/FreeSql/Extensions/FreeSqlGlobalExtensions.cs b/FreeSql/Extensions/FreeSqlGlobalExtensions.cs
index 12012b83..06db0959 100644
--- a/FreeSql/Extensions/FreeSqlGlobalExtensions.cs
+++ b/FreeSql/Extensions/FreeSqlGlobalExtensions.cs
@@ -1,5 +1,6 @@
using FreeSql;
using FreeSql.DataAnnotations;
+using FreeSql.Internal;
using FreeSql.Internal.CommonProvider;
using FreeSql.Internal.Model;
using FreeSql.Internal.ObjectPool;
@@ -1299,4 +1300,18 @@ SELECT ");
return NativeTuple.Create(query, af, sql);
}
#endregion
+
+ #region DynamicEntity
+#if net40 || NETSTANDARD2_0
+#else
+ ///
+ /// 动态构建Class Type
+ ///
+ ///
+ public static DynamicCompileBuilder DynamicEntity(this ICodeFirst codeFirst, string className, TableAttribute tableAttribute)
+ {
+ return new DynamicCompileBuilder(className, tableAttribute);
+ }
+#endif
+ #endregion
}
diff --git a/FreeSql/Extensions/TypeExtensions.cs b/FreeSql/Extensions/TypeExtensions.cs
deleted file mode 100644
index a5b75fd8..00000000
--- a/FreeSql/Extensions/TypeExtensions.cs
+++ /dev/null
@@ -1,40 +0,0 @@
-using FreeSql.Internal;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Reflection;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace DynamicBuilder
-{
-#if net40 || NETSTANDARD2_0
-#else
- public static class TypeExtensions
- {
- ///
- /// 根据动态构建的Class Type生成实例并进行属性赋值
- ///
- ///
- ///
- ///
- public static object CreateDynamicEntityInstance(this Type type, IFreeSql fsql,
- Dictionary porpertys)
- {
- return DynamicCompileBuilder.CreateObjectByTypeByCodeFirst(fsql, type, porpertys);
- }
-
- ///
- /// 设置对象属性值
- ///
- ///
- ///
- public static void SetPropertyValue(this Type type, IFreeSql fsql, ref object obj, string propertyName,
- object propertyValue)
- {
- var table = fsql.CodeFirst.GetTableByEntity(obj.GetType());
- table.ColumnsByCs[propertyName].SetValue(obj, propertyValue);
- }
- }
-#endif
-}
diff --git a/FreeSql/Internal/DynamicCompileBuilder.cs b/FreeSql/Internal/DynamicCompileBuilder.cs
index 9dfdd0cf..fb4903de 100644
--- a/FreeSql/Internal/DynamicCompileBuilder.cs
+++ b/FreeSql/Internal/DynamicCompileBuilder.cs
@@ -18,20 +18,19 @@ namespace FreeSql.Internal
public class DynamicCompileBuilder
{
private string _className = string.Empty;
- private TableAttribute _tableAttribute = null;
+ private Attribute[] _tableAttributes = null;
private List _properties = new List();
///
/// 配置Class
///
/// 类名
- /// 类标记的特性[Table(Name = "xxx")]
+ /// 类标记的特性[Table(Name = "xxx")]
///
- public DynamicCompileBuilder SetClass(string className, TableAttribute tableAttribute)
+ public DynamicCompileBuilder(string className, params Attribute[] attributes)
{
_className = className;
- _tableAttribute = tableAttribute;
- return this;
+ _tableAttributes = attributes;
}
///
@@ -54,22 +53,21 @@ namespace FreeSql.Internal
private void SetTableAttribute(ref TypeBuilder typeBuilder)
{
- var classCtorInfo = typeof(TableAttribute).GetConstructor(new Type[] { });
- var propertyInfos = typeof(TableAttribute).GetProperties().Where(p => p.CanWrite == true).ToArray();
- if (_tableAttribute == null)
- {
- return;
- }
+ if (_tableAttributes == null) return;
var propertyValues = new ArrayList();
- foreach (var propertyInfo in _tableAttribute.GetType().GetProperties().Where(p => p.CanWrite == true))
+ foreach (var tableAttribute in _tableAttributes)
{
- propertyValues.Add(propertyInfo.GetValue(_tableAttribute));
- }
+ if (tableAttribute == null) continue;
- var customAttributeBuilder =
- new CustomAttributeBuilder(classCtorInfo, new object[0], propertyInfos, propertyValues.ToArray());
- typeBuilder.SetCustomAttribute(customAttributeBuilder);
+ var classCtorInfo = tableAttribute.GetType().GetConstructor(new Type[] { });
+ var propertyInfos = tableAttribute.GetType().GetProperties().Where(p => p.CanWrite == true).ToArray();
+ foreach (var propertyInfo in propertyInfos)
+ propertyValues.Add(propertyInfo.GetValue(tableAttribute));
+
+ var customAttributeBuilder = new CustomAttributeBuilder(classCtorInfo, new object[0], propertyInfos, propertyValues.ToArray());
+ typeBuilder.SetCustomAttribute(customAttributeBuilder);
+ }
}
private void SetPropertys(ref TypeBuilder typeBuilder)
@@ -100,8 +98,7 @@ namespace FreeSql.Internal
ilOfSet.Emit(OpCodes.Ret);
//设置属性
- var propertyBuilder =
- typeBuilder.DefineProperty(propertyName, PropertyAttributes.None, propertyType, null);
+ var propertyBuilder = typeBuilder.DefineProperty(propertyName, PropertyAttributes.None, propertyType, null);
propertyBuilder.SetGetMethod(methodGet);
propertyBuilder.SetSetMethod(methodSet);
@@ -115,19 +112,15 @@ namespace FreeSql.Internal
private void SetPropertyAttribute(ref PropertyBuilder propertyBuilder, T tAttribute)
{
- if (tAttribute == null)
- return;
-
- var propertyValues = new ArrayList();
- foreach (var propertyInfo in tAttribute.GetType().GetProperties().Where(p => p.CanWrite == true))
- {
- propertyValues.Add(propertyInfo.GetValue(tAttribute));
- }
+ if (tAttribute == null) return;
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());
+ var propertyValues = new ArrayList();
+ foreach (var propertyInfo in propertyInfos)
+ propertyValues.Add(propertyInfo.GetValue(tAttribute));
+
+ var customAttributeBuilder = new CustomAttributeBuilder(constructor, new object[0], propertyInfos, propertyValues.ToArray());
propertyBuilder.SetCustomAttribute(customAttributeBuilder);
}
@@ -259,12 +252,13 @@ namespace FreeSql.Internal
return result;
}
+
+ class DynamicPropertyInfo
+ {
+ public string PropertyName { get; set; } = string.Empty;
+ public Type PropertyType { get; set; }
+ public Attribute[] Attributes { get; set; }
+ }
}
#endif
- internal class DynamicPropertyInfo
- {
- public string PropertyName { get; set; } = string.Empty;
- public Type PropertyType { get; set; }
- public Attribute[] Attributes { get; set; }
- }
}
\ No newline at end of file