一、使用
class Program { static void Main(string[] args) { User u1 = new User(); u1.UserName = "aaaaaaa"; u1.Salary = 89.63m; u1.BirthDay = DateTime.Now; u1.Age = 88; User u2 = new ObjectMapping<User, User>().Mapping(u1); Console.WriteLine("U2.UserName="+u2.UserName); Console.WriteLine("U2.Age=" + u2.Age); Console.WriteLine("U2.Salary=" + u2.Salary); Console.WriteLine("U2.BirthDay=" + u2.BirthDay); Console.Read(); } }
二、类
/// <summary> /// 两个对象之间的映射 /// 此类未经优化,只是一个核心实现 /// </summary> public class ObjectMapping<S, T> where S :class, new() where T:class, new() { public T Mapping(S source) { if (source == null) { return null; } T t = Activator.CreateInstance<T>(); Type tType = t.GetType(); Type sType = source.GetType(); PropertyInfo[] proertyInfos = tType.GetProperties(); if (!proertyInfos.Any()) { return null; } foreach (var pro in proertyInfos) { var sMethodInfo = sType.GetMethod("get_" + pro.Name); var tMethodInfo = tType.GetMethod("set_" + pro.Name); if (sMethodInfo == null || tMethodInfo == null) { continue; } var sFunc = GetFuncByMethodInfo(sMethodInfo, source); var taction = GetActionByMethodInfo(tMethodInfo, t); taction.DynamicInvoke(sFunc.DynamicInvoke()); } return t; } /// <summary> /// 获取 一个方法的 FUNC /// </summary> /// <param name="methodInfo"></param> /// <param name="obj"></param> /// <returns></returns> public Delegate GetFuncByMethodInfo(MethodInfo methodInfo,Object obj) { var parameters = new List<Type> (); parameters.AddRange(methodInfo.GetParameters().Select(p => p.ParameterType)); parameters.Add(methodInfo.ReturnType); var funcType= Expression.GetFuncType(parameters.ToArray<Type>()); return Delegate.CreateDelegate(funcType, obj, methodInfo); } /// <summary> /// 获取一个方法的 ACTION /// </summary> /// <param name="methodInfo"></param> /// <param name="obj"></param> /// <returns></returns> public Delegate GetActionByMethodInfo(MethodInfo methodInfo, Object obj) { var parameters = new List<Type>(); parameters.AddRange(methodInfo.GetParameters().Select(p => p.ParameterType)); var actionType = Expression.GetActionType(parameters.ToArray<Type>()); return Delegate.CreateDelegate(actionType, obj, methodInfo); } }
相关知识:
Delegate.CreateDelegate 方法
创建指定类型的委托。
程序集: mscorlib(位于 mscorlib.dll)
名称 | 说明 | |
---|---|---|
CreateDelegate(Type, MethodInfo) |
创建指定类型的委托以表示指定的静态方法。 | |
CreateDelegate(Type, MethodInfo, Boolean) |
使用针对绑定失败的指定行为,创建用于表示指定静态方法的指定类型的委托。 | |
CreateDelegate(Type, Object, MethodInfo) |
使用指定的第一个参数创建指定类型的委托,该委托表示指定的静态方法或实例方法。 | |
CreateDelegate(Type, Object, MethodInfo, Boolean) |
使用指定的第一个参数和针对绑定失败的指定行为,创建表示指定的静态方法或实例方法的指定类型的委托。 | |
CreateDelegate(Type, Object, String) |
创建指定类型的委托,该委托表示要对指定的类实例调用的指定实例方法。 | |
CreateDelegate(Type, Object, String, Boolean) |
创建指定类型的委托,该委托表示要按指定的大小写敏感度对指定类实例调用的指定实例方法。 | |
CreateDelegate(Type, Object, String, Boolean, Boolean) |
使用用于指定是否区分大小写的值和针对绑定失败的指定行为,创建指定类型的委托,该委托表示要对指定类实例调用的指定实例方法。 | |
CreateDelegate(Type, Type, String) |
创建指定类型的委托,该委托表示指定类的指定静态方法。 | |
CreateDelegate(Type, Type, String, Boolean) |
使用用于指定是否区分大小写的值创建指定类型的委托,该委托表示指定类的指定静态方法。 | |
CreateDelegate(Type, Type, String, Boolean, Boolean) |
使用用于指定是否区分大小写的值和针对绑定失败的指定行为,创建指定类型的委托,该委托表示指定类的指定静态方法。 |
Expression.TryGetFuncType 方法 (Type[], Type)
创建一个 Type 对象,它表示具有特定类型参数的泛型 System.Func 委托类型。 最后一个类型参数指定已创建委托的返回类型。
程序集: System.Core(位于 System.Core.dll)