77using System . Linq . Expressions ;
88using System . Reflection ;
99using System . Reflection . Emit ;
10+ using System . Security . Cryptography ;
11+ using FreeSql . Internal . Model ;
12+ using System . Text ;
1013
1114namespace FreeSql . Internal
1215{
1316#if net40 || NETSTANDARD2_0
14-
1517#else
16- public class DynamicCompileBuilder
18+ public class DynamicCompileBuilder
1719 {
1820 private string _className = string . Empty ;
1921 private TableAttribute _tableAttribute = null ;
@@ -39,12 +41,12 @@ public DynamicCompileBuilder SetClass(string className, TableAttribute tableAttr
3941 /// <param name="propertyType">属性类型</param>
4042 /// <param name="attributes">属性标记的特性-支持多个</param>
4143 /// <returns></returns>
42- public DynamicCompileBuilder Property ( string propertyName , Type propertyType , params Attribute [ ] attributes )
44+ public DynamicCompileBuilder Property ( string propertyName , Type propertyType , params Attribute [ ] attributes )
4345 {
4446 _properties . Add ( new DynamicPropertyInfo ( )
4547 {
4648 PropertyName = propertyName ,
47- PropertyType = propertyType ,
49+ PropertyType = propertyType ,
4850 Attributes = attributes
4951 } ) ;
5052 return this ;
@@ -140,7 +142,8 @@ public Type Build()
140142 //设置程序集的名称
141143 var defineDynamicAssembly = AssemblyBuilder . DefineDynamicAssembly ( assemblyName , AssemblyBuilderAccess . Run ) ;
142144 //动态在程序集内创建一个模块
143- var defineDynamicModule = defineDynamicAssembly . DefineDynamicModule ( "FreeSql.DynamicCompileBuilder.Dynamics" ) ;
145+ var defineDynamicModule =
146+ defineDynamicAssembly . DefineDynamicModule ( "FreeSql.DynamicCompileBuilder.Dynamics" ) ;
144147 //动态的在模块内创建一个类
145148 var typeBuilder = defineDynamicModule . DefineType ( _className , TypeAttributes . Public | TypeAttributes . Class ) ;
146149
@@ -158,7 +161,25 @@ public Type Build()
158161 private static ConcurrentDictionary < string , Delegate >
159162 _delegateCache = new ConcurrentDictionary < string , Delegate > ( ) ;
160163
161- //设置动态对象的属性值
164+ //设置动态对象的属性值 使用FreeSql自带功能
165+ public static object CreateObjectByTypeByCodeFirst ( IFreeSql fsql , Type type ,
166+ Dictionary < string , object > porpertys )
167+ {
168+ if ( type == null )
169+ return null ;
170+ object istance = Activator . CreateInstance ( type ) ;
171+ if ( istance == null )
172+ return null ;
173+ var table = fsql . CodeFirst . GetTableByEntity ( type ) ;
174+ foreach ( var kv in porpertys )
175+ {
176+ table . ColumnsByCs [ kv . Key ] . SetValue ( istance , kv . Value ) ;
177+ }
178+
179+ return istance ;
180+ }
181+
182+ //设置动态对象的属性值,使用表达式目录树
162183 public static object CreateObjectByType ( Type type , Dictionary < string , object > porpertys )
163184 {
164185 if ( type == null )
@@ -167,7 +188,8 @@ public static object CreateObjectByType(Type type, Dictionary<string, object> po
167188 if ( istance == null )
168189 return null ;
169190 //根据字典中的key确定缓存
170- var cacheKey = string . Join ( "-" , porpertys . Keys . OrderBy ( s => s ) ) ;
191+ var cacheKeyStr = string . Join ( "-" , porpertys . Keys . OrderBy ( s => s ) ) ;
192+ var cacheKey = Md5Encryption ( cacheKeyStr ) ;
171193 var dynamicDelegate = _delegateCache . GetOrAdd ( cacheKey , key =>
172194 {
173195 //表达式目录树构建委托
@@ -224,13 +246,25 @@ private string FirstCharToUpper(string input)
224246 string str = input . First ( ) . ToString ( ) . ToUpper ( ) + input . Substring ( 1 ) ;
225247 return str ;
226248 }
249+
250+ private static string Md5Encryption ( string inputStr )
251+ {
252+ var result = string . Empty ;
253+ //32位大写
254+ using ( var md5 = MD5 . Create ( ) )
255+ {
256+ var resultBytes = md5 . ComputeHash ( Encoding . UTF8 . GetBytes ( inputStr ) ) ;
257+ result = BitConverter . ToString ( resultBytes ) ;
258+ }
259+
260+ return result ;
261+ }
227262 }
228263#endif
229264 internal class DynamicPropertyInfo
230265 {
231266 public string PropertyName { get ; set ; } = string . Empty ;
232267 public Type PropertyType { get ; set ; }
233- public Attribute [ ] Attributes { get ; set ; }
268+ public Attribute [ ] Attributes { get ; set ; }
234269 }
235- }
236-
270+ }
0 commit comments