Skip to content

Commit 18cd0e7

Browse files
committed
- 优化DynamicEntity设置动态对象的属性值逻辑
1 parent fa20468 commit 18cd0e7

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

Extensions/FreeSql.Extensions.DynamicEntity/DynamicCompileBuilder.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
using System;
22
using System.Collections;
3-
using System.Collections.Concurrent;
43
using System.Collections.Generic;
54
using System.Linq;
6-
using System.Linq.Expressions;
75
using System.Reflection;
86
using System.Reflection.Emit;
97
using System.Security.Cryptography;
108
using System.Text;
119

12-
namespace FreeSql
10+
namespace FreeSql.Extensions.DynamicEntity
1311
{
1412
/// <summary>
1513
/// 动态创建实体类型
@@ -57,7 +55,7 @@ public DynamicCompileBuilder Property(string propertyName, Type propertyType, pa
5755
/// </summary>
5856
/// <param name="superClass">父类类型</param>
5957
/// <returns></returns>
60-
public DynamicCompileBuilder SuperClass(Type superClass)
58+
public DynamicCompileBuilder Extend(Type superClass)
6159
{
6260
_superClass = superClass;
6361
return this;

Extensions/FreeSql.Extensions.DynamicEntity/DynamicCompileHelper.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
4+
using System.Linq;
45
using System.Linq.Expressions;
6+
using System.Security.Cryptography;
7+
using System.Text;
58

69
namespace FreeSql.Extensions.DynamicEntity
710
{
@@ -22,8 +25,8 @@ public static DynamicCompileBuilder DynamicBuilder()
2225
/// <summary>
2326
/// 委托缓存
2427
/// </summary>
25-
private static readonly ConcurrentDictionary<string, Delegate>
26-
DelegateCache = new ConcurrentDictionary<string, Delegate>();
28+
private static readonly ConcurrentDictionary<string, Delegate> DelegateCache =
29+
new ConcurrentDictionary<string, Delegate>();
2730

2831
/// <summary>
2932
/// 设置动态对象的属性值
@@ -39,7 +42,9 @@ public static object CreateObjectByType(Type type, Dictionary<string, object> po
3942
if (istance == null)
4043
return null;
4144
//根据key确定缓存
42-
var cacheKey = $"{type.GetHashCode()}{porpertys.GetHashCode()}";
45+
var cacheKeyStr = string.Join("-", porpertys.Keys.OrderBy(s => s));
46+
var dicKey = Md5Encryption(cacheKeyStr);
47+
var cacheKey = $"{type.GetHashCode()}-{dicKey}";
4348
var dynamicDelegate = DelegateCache.GetOrAdd(cacheKey, key =>
4449
{
4550
//表达式目录树构建委托
@@ -70,5 +75,18 @@ public static object CreateObjectByType(Type type, Dictionary<string, object> po
7075
var dynamicInvoke = dynamicDelegate.DynamicInvoke(istance, porpertys);
7176
return dynamicInvoke;
7277
}
78+
79+
private static string Md5Encryption(string inputStr)
80+
{
81+
var result = string.Empty;
82+
//32位大写
83+
using (var md5 = MD5.Create())
84+
{
85+
var resultBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(inputStr));
86+
result = BitConverter.ToString(resultBytes);
87+
}
88+
89+
return result;
90+
}
7391
}
7492
}

Extensions/FreeSql.Extensions.DynamicEntity/FreeSql.Extensions.DynamicEntity.xml

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)