- 动态操作表结构相关的 API

This commit is contained in:
2881099 2023-05-04 21:46:11 +08:00
parent a0f4c9022d
commit 235a2c2636
4 changed files with 44 additions and 101 deletions

View File

@ -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
{
/// <summary>
/// 动态构建Class Type
/// </summary>
/// <returns></returns>
public static DynamicCompileBuilder DynamicEntity(this ICodeFirst codeFirst, string className,
TableAttribute tableAttribute)
{
return new DynamicCompileBuilder().SetClass(className, tableAttribute);
}
}
#endif
}

View File

@ -1,5 +1,6 @@
using FreeSql; using FreeSql;
using FreeSql.DataAnnotations; using FreeSql.DataAnnotations;
using FreeSql.Internal;
using FreeSql.Internal.CommonProvider; using FreeSql.Internal.CommonProvider;
using FreeSql.Internal.Model; using FreeSql.Internal.Model;
using FreeSql.Internal.ObjectPool; using FreeSql.Internal.ObjectPool;
@ -1299,4 +1300,18 @@ SELECT ");
return NativeTuple.Create(query, af, sql); return NativeTuple.Create(query, af, sql);
} }
#endregion #endregion
#region DynamicEntity
#if net40 || NETSTANDARD2_0
#else
/// <summary>
/// 动态构建Class Type
/// </summary>
/// <returns></returns>
public static DynamicCompileBuilder DynamicEntity(this ICodeFirst codeFirst, string className, TableAttribute tableAttribute)
{
return new DynamicCompileBuilder(className, tableAttribute);
}
#endif
#endregion
} }

View File

@ -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
{
/// <summary>
/// 根据动态构建的Class Type生成实例并进行属性赋值
/// </summary>
/// <param name="type"></param>
/// <param name="porpertys"></param>
/// <returns></returns>
public static object CreateDynamicEntityInstance(this Type type, IFreeSql fsql,
Dictionary<string, object> porpertys)
{
return DynamicCompileBuilder.CreateObjectByTypeByCodeFirst(fsql, type, porpertys);
}
/// <summary>
/// 设置对象属性值
/// </summary>
/// <param name="fsql"></param>
/// <returns></returns>
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
}

View File

@ -18,20 +18,19 @@ namespace FreeSql.Internal
public class DynamicCompileBuilder public class DynamicCompileBuilder
{ {
private string _className = string.Empty; private string _className = string.Empty;
private TableAttribute _tableAttribute = null; private Attribute[] _tableAttributes = null;
private List<DynamicPropertyInfo> _properties = new List<DynamicPropertyInfo>(); private List<DynamicPropertyInfo> _properties = new List<DynamicPropertyInfo>();
/// <summary> /// <summary>
/// 配置Class /// 配置Class
/// </summary> /// </summary>
/// <param name="className">类名</param> /// <param name="className">类名</param>
/// <param name="tableAttribute">类标记的特性[Table(Name = "xxx")]</param> /// <param name="attributes">类标记的特性[Table(Name = "xxx")]</param>
/// <returns></returns> /// <returns></returns>
public DynamicCompileBuilder SetClass(string className, TableAttribute tableAttribute) public DynamicCompileBuilder(string className, params Attribute[] attributes)
{ {
_className = className; _className = className;
_tableAttribute = tableAttribute; _tableAttributes = attributes;
return this;
} }
/// <summary> /// <summary>
@ -54,23 +53,22 @@ namespace FreeSql.Internal
private void SetTableAttribute(ref TypeBuilder typeBuilder) private void SetTableAttribute(ref TypeBuilder typeBuilder)
{ {
var classCtorInfo = typeof(TableAttribute).GetConstructor(new Type[] { }); if (_tableAttributes == null) return;
var propertyInfos = typeof(TableAttribute).GetProperties().Where(p => p.CanWrite == true).ToArray();
if (_tableAttribute == null)
{
return;
}
var propertyValues = new ArrayList(); 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 = var classCtorInfo = tableAttribute.GetType().GetConstructor(new Type[] { });
new CustomAttributeBuilder(classCtorInfo, new object[0], propertyInfos, propertyValues.ToArray()); 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); typeBuilder.SetCustomAttribute(customAttributeBuilder);
} }
}
private void SetPropertys(ref TypeBuilder typeBuilder) private void SetPropertys(ref TypeBuilder typeBuilder)
{ {
@ -100,8 +98,7 @@ namespace FreeSql.Internal
ilOfSet.Emit(OpCodes.Ret); ilOfSet.Emit(OpCodes.Ret);
//设置属性 //设置属性
var propertyBuilder = var propertyBuilder = typeBuilder.DefineProperty(propertyName, PropertyAttributes.None, propertyType, null);
typeBuilder.DefineProperty(propertyName, PropertyAttributes.None, propertyType, null);
propertyBuilder.SetGetMethod(methodGet); propertyBuilder.SetGetMethod(methodGet);
propertyBuilder.SetSetMethod(methodSet); propertyBuilder.SetSetMethod(methodSet);
@ -115,19 +112,15 @@ namespace FreeSql.Internal
private void SetPropertyAttribute<T>(ref PropertyBuilder propertyBuilder, T tAttribute) private void SetPropertyAttribute<T>(ref PropertyBuilder propertyBuilder, T tAttribute)
{ {
if (tAttribute == null) if (tAttribute == null) return;
return;
var propertyValues = new ArrayList();
foreach (var propertyInfo in tAttribute.GetType().GetProperties().Where(p => p.CanWrite == true))
{
propertyValues.Add(propertyInfo.GetValue(tAttribute));
}
var propertyInfos = tAttribute.GetType().GetProperties().Where(p => p.CanWrite == true).ToArray(); var propertyInfos = tAttribute.GetType().GetProperties().Where(p => p.CanWrite == true).ToArray();
var constructor = tAttribute.GetType().GetConstructor(new Type[] { }); var constructor = tAttribute.GetType().GetConstructor(new Type[] { });
var customAttributeBuilder = var propertyValues = new ArrayList();
new CustomAttributeBuilder(constructor, new object[0], propertyInfos, propertyValues.ToArray()); foreach (var propertyInfo in propertyInfos)
propertyValues.Add(propertyInfo.GetValue(tAttribute));
var customAttributeBuilder = new CustomAttributeBuilder(constructor, new object[0], propertyInfos, propertyValues.ToArray());
propertyBuilder.SetCustomAttribute(customAttributeBuilder); propertyBuilder.SetCustomAttribute(customAttributeBuilder);
} }
@ -259,12 +252,13 @@ namespace FreeSql.Internal
return result; return result;
} }
}
#endif class DynamicPropertyInfo
internal class DynamicPropertyInfo
{ {
public string PropertyName { get; set; } = string.Empty; public string PropertyName { get; set; } = string.Empty;
public Type PropertyType { get; set; } public Type PropertyType { get; set; }
public Attribute[] Attributes { get; set; } public Attribute[] Attributes { get; set; }
} }
} }
#endif
}