寻找一种快速简便的方法来合并POCO上的所有属性

我有一些简单的类,有一堆简单的属性(简单的{get; set;}声明)。 所有属性都可以为空(或等效地,引用类型)。

例如:

 class POCO { int? Field1 { get; set; } string Field2 { get; set; } ... etc ... } 

我有一个场景,我正在逐步构建这些POCO,最后我想让其中一个包含所有非空字段。

一些说明性代码:

 POCO o1 = LoadFields1To3(); POCO o2 = LoadFields4To5(); POCO o3 = LoadFields6To9(); ... etc ... 

我们在这种情况下是因为某些字段是从SQL加载的(有时是不同的查询),而有些是从内存数据结构中加载的。 我在这里重新使用POCO类型来避免一堆无意义的类(静态类型对于Dapper来说非常有用,而且一般来说)。

我正在寻找的是将这些对象的属性合并为具有非null属性的单个属性的好方法。

就像是:

 POCO final = o1.UnionProperties(o2).UnionProperties(o3) // and so on 

我能够保证在多个对象上没有字段为非null。 虽然我假设一个解决方案会采用最左边的非空字段,但实际上并不是必需的。

我知道我可以写一些reflection代码来做到这一点,但它有点讨厌和缓慢。

这确实需要一般适用,因为虽然我从不打算合并不同类型的对象,但是这种方法适用的类型非常多。

我想知道是否有一些更聪明的方式,也许 AB 用动态?

我收集(好吧,我问过你)这里的主要目标是:

  • 表现(reflection似乎太慢)
  • 低维护(想要避免非常手动的复制方法,或复杂的属性)

以下使用元编程在运行时动态执行任何操作,将自身编译为类型化委托( Action )以进行有效重用:

 using System; using System.Collections.Generic; using System.Reflection.Emit; public class SamplePoco { public int? Field1 { get; set; } public string Field2 { get; set; } // lots and lots more properties here } static class Program { static void Main() { var obj1 = new SamplePoco { Field1 = 123 }; var obj2 = new SamplePoco { Field2 = "abc" }; var merged = Merger.Merge(obj1, obj2); Console.WriteLine(merged.Field1); Console.WriteLine(merged.Field2); } } static class Merger { public static T Merge(params T[] sources) where T : class, new() { var merge = MergerImpl.merge; var obj = new T(); for (int i = 0; i < sources.Length; i++) merge(sources[i], obj); return obj; } static class MergerImpl where T : class, new() { internal static readonly Action merge; static MergerImpl() { var method = new DynamicMethod("Merge", null, new[] { typeof(T), typeof(T) }, typeof(T)); var il = method.GetILGenerator(); Dictionary locals = new Dictionary(); foreach (var prop in typeof(T).GetProperties()) { var propType = prop.PropertyType; if (propType.IsValueType && Nullable.GetUnderlyingType(propType) == null) { continue; // int, instead of int? etc - skip } il.Emit(OpCodes.Ldarg_1); // [target] il.Emit(OpCodes.Ldarg_0); // [target][source] il.EmitCall(OpCodes.Callvirt, prop.GetGetMethod(), null); // [target][value] il.Emit(OpCodes.Dup); // [target][value][value] Label nonNull = il.DefineLabel(), end = il.DefineLabel(); if (propType.IsValueType) { // int? etc - Nullable - hit .Value LocalBuilder local; if (!locals.TryGetValue(propType, out local)) { local = il.DeclareLocal(propType); locals.Add(propType, local); } // need a ref to use it for the static-call il.Emit(OpCodes.Stloc, local); // [target][value] il.Emit(OpCodes.Ldloca, local); // [target][value][value*] var hasValue = propType.GetProperty("HasValue").GetGetMethod(); il.EmitCall(OpCodes.Call, hasValue, null); // [target][value][value.HasValue] } il.Emit(OpCodes.Brtrue_S, nonNull); // [target][value] il.Emit(OpCodes.Pop); // [target] il.Emit(OpCodes.Pop); // nix il.Emit(OpCodes.Br_S, end); // nix il.MarkLabel(nonNull); // (incoming) [target][value] il.EmitCall(OpCodes.Callvirt, prop.GetSetMethod(), null); // nix il.MarkLabel(end); // (incoming) nix } il.Emit(OpCodes.Ret); merge = (Action)method.CreateDelegate(typeof(Action)); } } }