mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 10:42:52 +08:00
动态操作表结构相关的API
This commit is contained in:
parent
8bd85ebe22
commit
5046907e4f
@ -179,47 +179,47 @@ namespace FreeSql.Internal
|
||||
return istance;
|
||||
}
|
||||
|
||||
//设置动态对象的属性值,使用表达式目录树
|
||||
public static object CreateObjectByType(Type type, Dictionary<string, object> porpertys)
|
||||
{
|
||||
if (type == null)
|
||||
return null;
|
||||
object istance = Activator.CreateInstance(type);
|
||||
if (istance == null)
|
||||
return null;
|
||||
//根据字典中的key确定缓存
|
||||
var cacheKeyStr = string.Join("-", porpertys.Keys.OrderBy(s => s));
|
||||
var cacheKey = Md5Encryption(cacheKeyStr);
|
||||
var dynamicDelegate = _delegateCache.GetOrAdd(cacheKey, key =>
|
||||
{
|
||||
//表达式目录树构建委托
|
||||
var typeParam = Expression.Parameter(type);
|
||||
var dicParamType = typeof(Dictionary<string, object>);
|
||||
var dicParam = Expression.Parameter(dicParamType);
|
||||
var exps = new List<Expression>();
|
||||
var tempRef = Expression.Variable(typeof(object));
|
||||
foreach (var pinfo in porpertys)
|
||||
{
|
||||
var propertyInfo = type.GetProperty(pinfo.Key);
|
||||
if (propertyInfo == null)
|
||||
continue;
|
||||
var propertyName = Expression.Constant(pinfo.Key, typeof(string));
|
||||
exps.Add(Expression.Call(dicParam, dicParamType.GetMethod("TryGetValue"), propertyName, tempRef));
|
||||
exps.Add(Expression.Assign(Expression.MakeMemberAccess(typeParam, propertyInfo),
|
||||
Expression.Convert(tempRef, propertyInfo.PropertyType)));
|
||||
exps.Add(Expression.Assign(tempRef, Expression.Default(typeof(object))));
|
||||
}
|
||||
////设置动态对象的属性值,使用表达式目录树
|
||||
//public static object CreateObjectByType(Type type, Dictionary<string, object> porpertys)
|
||||
//{
|
||||
// if (type == null)
|
||||
// return null;
|
||||
// object istance = Activator.CreateInstance(type);
|
||||
// if (istance == null)
|
||||
// return null;
|
||||
// //根据字典中的key确定缓存
|
||||
// var cacheKeyStr = string.Join("-", porpertys.Keys.OrderBy(s => s));
|
||||
// var cacheKey = Md5Encryption(cacheKeyStr);
|
||||
// var dynamicDelegate = _delegateCache.GetOrAdd(cacheKey, key =>
|
||||
// {
|
||||
// //表达式目录树构建委托
|
||||
// var typeParam = Expression.Parameter(type);
|
||||
// var dicParamType = typeof(Dictionary<string, object>);
|
||||
// var dicParam = Expression.Parameter(dicParamType);
|
||||
// var exps = new List<Expression>();
|
||||
// var tempRef = Expression.Variable(typeof(object));
|
||||
// foreach (var pinfo in porpertys)
|
||||
// {
|
||||
// var propertyInfo = type.GetProperty(pinfo.Key);
|
||||
// if (propertyInfo == null)
|
||||
// continue;
|
||||
// var propertyName = Expression.Constant(pinfo.Key, typeof(string));
|
||||
// exps.Add(Expression.Call(dicParam, dicParamType.GetMethod("TryGetValue"), propertyName, tempRef));
|
||||
// exps.Add(Expression.Assign(Expression.MakeMemberAccess(typeParam, propertyInfo),
|
||||
// Expression.Convert(tempRef, propertyInfo.PropertyType)));
|
||||
// exps.Add(Expression.Assign(tempRef, Expression.Default(typeof(object))));
|
||||
// }
|
||||
|
||||
var returnTarget = Expression.Label(type);
|
||||
exps.Add(Expression.Return(returnTarget, typeParam));
|
||||
exps.Add(Expression.Label(returnTarget, Expression.Default(type)));
|
||||
var block = Expression.Block(new[] { tempRef }, exps);
|
||||
var @delegate = Expression.Lambda(block, typeParam, dicParam).Compile();
|
||||
return @delegate;
|
||||
});
|
||||
var dynamicInvoke = dynamicDelegate.DynamicInvoke(istance, porpertys);
|
||||
return dynamicInvoke;
|
||||
}
|
||||
// var returnTarget = Expression.Label(type);
|
||||
// exps.Add(Expression.Return(returnTarget, typeParam));
|
||||
// exps.Add(Expression.Label(returnTarget, Expression.Default(type)));
|
||||
// var block = Expression.Block(new[] { tempRef }, exps);
|
||||
// var @delegate = Expression.Lambda(block, typeParam, dicParam).Compile();
|
||||
// return @delegate;
|
||||
// });
|
||||
// var dynamicInvoke = dynamicDelegate.DynamicInvoke(istance, porpertys);
|
||||
// return dynamicInvoke;
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// 首字母小写
|
||||
|
Loading…
x
Reference in New Issue
Block a user