调整动态赋值逻辑

This commit is contained in:
d4ilys 2023-04-25 10:27:09 +08:00
parent c012c166d8
commit 1753a51b42

View File

@ -7,13 +7,15 @@ using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
using System.Reflection.Emit; using System.Reflection.Emit;
using System.Security.Cryptography;
using FreeSql.Internal.Model;
using System.Text;
namespace FreeSql.Internal namespace FreeSql.Internal
{ {
#if net40 || NETSTANDARD2_0 #if net40 || NETSTANDARD2_0
#else #else
public class DynamicCompileBuilder public class DynamicCompileBuilder
{ {
private string _className = string.Empty; private string _className = string.Empty;
private TableAttribute _tableAttribute = null; private TableAttribute _tableAttribute = null;
@ -39,12 +41,12 @@ namespace FreeSql.Internal
/// <param name="propertyType">属性类型</param> /// <param name="propertyType">属性类型</param>
/// <param name="attributes">属性标记的特性-支持多个</param> /// <param name="attributes">属性标记的特性-支持多个</param>
/// <returns></returns> /// <returns></returns>
public DynamicCompileBuilder Property(string propertyName, Type propertyType, params Attribute [] attributes) 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,
Attributes = attributes Attributes = attributes
}); });
return this; return this;
@ -140,7 +142,8 @@ namespace FreeSql.Internal
//设置程序集的名称 //设置程序集的名称
var defineDynamicAssembly = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); var defineDynamicAssembly = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
//动态在程序集内创建一个模块 //动态在程序集内创建一个模块
var defineDynamicModule = defineDynamicAssembly.DefineDynamicModule("FreeSql.DynamicCompileBuilder.Dynamics"); var defineDynamicModule =
defineDynamicAssembly.DefineDynamicModule("FreeSql.DynamicCompileBuilder.Dynamics");
//动态的在模块内创建一个类 //动态的在模块内创建一个类
var typeBuilder = defineDynamicModule.DefineType(_className, TypeAttributes.Public | TypeAttributes.Class); var typeBuilder = defineDynamicModule.DefineType(_className, TypeAttributes.Public | TypeAttributes.Class);
@ -158,7 +161,25 @@ namespace FreeSql.Internal
private static ConcurrentDictionary<string, Delegate> private static ConcurrentDictionary<string, Delegate>
_delegateCache = new ConcurrentDictionary<string, Delegate>(); _delegateCache = new ConcurrentDictionary<string, Delegate>();
//设置动态对象的属性值 //设置动态对象的属性值 使用FreeSql自带功能
public static object CreateObjectByTypeByCodeFirst(IFreeSql fsql, Type type,
Dictionary<string, object> porpertys)
{
if (type == null)
return null;
object istance = Activator.CreateInstance(type);
if (istance == null)
return null;
var table = fsql.CodeFirst.GetTableByEntity(type);
foreach (var kv in porpertys)
{
table.ColumnsByCs[kv.Key].SetValue(istance, kv.Value);
}
return istance;
}
//设置动态对象的属性值,使用表达式目录树
public static object CreateObjectByType(Type type, Dictionary<string, object> porpertys) public static object CreateObjectByType(Type type, Dictionary<string, object> porpertys)
{ {
if (type == null) if (type == null)
@ -167,7 +188,8 @@ namespace FreeSql.Internal
if (istance == null) if (istance == null)
return null; return null;
//根据字典中的key确定缓存 //根据字典中的key确定缓存
var cacheKey = string.Join("-", porpertys.Keys.OrderBy(s => s)); var cacheKeyStr = string.Join("-", porpertys.Keys.OrderBy(s => s));
var cacheKey = Md5Encryption(cacheKeyStr);
var dynamicDelegate = _delegateCache.GetOrAdd(cacheKey, key => var dynamicDelegate = _delegateCache.GetOrAdd(cacheKey, key =>
{ {
//表达式目录树构建委托 //表达式目录树构建委托
@ -224,13 +246,25 @@ namespace FreeSql.Internal
string str = input.First().ToString().ToUpper() + input.Substring(1); string str = input.First().ToString().ToUpper() + input.Substring(1);
return str; return str;
} }
private static string Md5Encryption(string inputStr)
{
var result = string.Empty;
//32位大写
using (var md5 = MD5.Create())
{
var resultBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(inputStr));
result = BitConverter.ToString(resultBytes);
}
return result;
}
} }
#endif #endif
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; } public Type PropertyType { get; set; }
public Attribute [] Attributes { get; set; } public Attribute[] Attributes { get; set; }
} }
} }