using System;using System.Collections.Generic;using System.Linq;using System.Caching;using System.Text;using System.Extension;using System.Collections.Concurrent;namespace System.Linq.Expressions{ public class PropertyFieldLoader : CacheBlock > { protected PropertyFieldLoader() { } public object Load(object tObj, Type type, string propertyPath) { return Compile(type, propertyPath)(tObj); } public T Load (object tObj, Type type, string propertyPath) where T : class { return Load(tObj, type, propertyPath) as T; } public Func
2.更新属性
using System;using System.Collections.Generic;using System.Linq;using System.Caching;using System.Text;using System.Extension;using System.Collections.Concurrent;namespace System.Linq.Expressions{ public class PropertyFieldSetter : CacheBlock > { protected PropertyFieldSetter() { } public void Set(object obj, string propertyPath, object value) { var tObj = obj.GetType(); var tValue = value.GetType(); var act = Compile(tObj, tValue, propertyPath); act(obj, value); } public static PropertyFieldSetter Instance = new PropertyFieldSetter(); public Action Compile(Type typeObj,Type typeValue, string propertyPath) { var key = "Set_" + typeObj.FullName + "_" + typeValue.FullName; var act = ConcurrentDic.GetOrAdd(key + "." + propertyPath, (s) => { ParameterExpression paramInstance = Expression.Parameter(typeof(object), "obj"); ParameterExpression paramValue = Expression.Parameter(typeof(object), "value"); Expression expression = Expression.Convert(paramInstance, typeObj); foreach (var pro in propertyPath.Split('.')) expression = Expression.PropertyOrField(expression, pro); var value = Expression.Convert(paramValue, typeValue); expression = Expression.Assign(expression, value); var exp = Expression.Lambda >(expression, paramInstance, paramValue); return exp.Compile(); }); return act; } }}
3.数据转换
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Reflection;using System.Caching;namespace System.Linq.Expressions{ public class DataTransfer : CacheBlock > where T : new() { protected DataTransfer() { } public Func Compile(Type inType) { var outType = typeof(T); var pKey = "Transfer_" + inType.FullName + "_" + outType.FullName; var func = this.ConcurrentDic.GetOrAdd(pKey, (string ckey) => { var proOrFie = outType.GetProperties().Cast () .Union(outType.GetFields().Cast ()) .Union(inType.GetProperties().Cast ()) .Union(inType.GetFields().Cast ()) .GroupBy(i => i.Name).Where(i => i.Count() == 2) .ToDictionary(i => i.Key, i => i); var returnTarget = Expression.Label(outType); var returnLabel = Expression.Label(returnTarget, Expression.Default(outType)); var pramSource = Expression.Parameter(typeof(object)); var parmConverted = Expression.Convert(pramSource, inType); var newExp = Expression.New(outType); var variate = Expression.Parameter(outType, "instance"); var assVar = Expression.Assign(variate, newExp); Expression[] expressions = new Expression[proOrFie.Count + 3]; expressions[0] = assVar; var assExps = proOrFie.Keys.Select(i => { var value = Expression.PropertyOrField(parmConverted, i); var prop = Expression.PropertyOrField(variate, i); var assIgnExp = Expression.Assign(prop, value); return assIgnExp; }); var index = 1; foreach (var exp in assExps) { expressions[index] = exp; index++; } expressions[index] = Expression.Return(returnTarget,variate); expressions[index + 1] = returnLabel; var block = Expression.Block(new[] { variate }, expressions); var expression = Expression.Lambda >(block, pramSource); return expression.Compile(); }); return func; } public static DataTransfer Instance = new DataTransfer (); }}