C#动态/扩展对象的深度/嵌套/递归合并

本文关键字:嵌套 递归 深度 合并 对象 动态 扩展 | 更新日期: 2023-09-27 18:21:45

我需要在C#中"合并"2个动态对象。我在stackexchange上发现的所有内容都只涉及非递归合并。但我正在寻找能够进行递归或深度合并的东西,与jQuery的$.extend(obj1, obj2)函数非常相似。

当两个成员发生碰撞时,应适用以下规则:

  • 如果类型不匹配,则必须引发异常并中止合并。异常:obj2值可能为null,在这种情况下,值&使用obj1类型
  • 对于琐碎类型(值类型+字符串),总是首选obj1值
  • 对于非平凡类型,应用以下规则:
    • CCD_ 2&IEnumberables<T>被简单地合并(可能是.Concat()?)
    • IDictionary&CCD_ 6被合并;obj1关键帧在碰撞时具有优先级
    • CCD_ 7&Expando[]类型必须递归合并,而Expando[]将始终只有相同类型的元素
    • 可以假设集合中没有Expando对象(IEnumerabe&IDictionary)
  • 所有其他类型都可以丢弃,并且不必出现在生成的动态对象中

下面是一个可能合并的例子:

dynamic DefaultConfig = new {
    BlacklistedDomains = new string[] { "domain1.com" },
    ExternalConfigFile = "blacklist.txt",
    UseSockets = new[] {
        new { IP = "127.0.0.1", Port = "80"},
        new { IP = "127.0.0.2", Port = "8080" }
    }
};
dynamic UserSpecifiedConfig = new {
    BlacklistedDomain = new string[] { "example1.com" },
    ExternalConfigFile = "C:''my_blacklist.txt"
};
var result = Merge (UserSpecifiedConfig, DefaultConfig);
// result should now be equal to:
var result_equal = new {
    BlacklistedDomains = new string[] { "domain1.com", "example1.com" },
    ExternalConfigFile = "C:''my_blacklist.txt",
    UseSockets = new[] {
        new { IP = "127.0.0.1", Port = "80"},
        new { IP = "127.0.0.2", Port = "8080" }
    }
};

有什么办法吗?

C#动态/扩展对象的深度/嵌套/递归合并

好吧,这有点冗长,但请看一看。这是一个使用Reflection.Emit.的实现

对我来说,悬而未决的问题是如何实现ToString()重写,以便进行字符串比较。这些值是来自配置文件还是其他什么?如果它们是JSON格式的,我认为你可能会比使用JsonSerializer做得更糟。这取决于你想要什么。

你可以使用Expando对象来消除反射。也可以在循环的底部发出无意义的东西:

var result = new ExpandoObject();
var resultDict = result as IDictionary<string, object>;
foreach (string key in resVals.Keys)
{
    resultDict.Add(key, resVals[key]);
}
return result;

不过,我看不出有什么方法可以绕过解析原始对象树的混乱代码,而不是立即解析。我想听听其他人对此的看法。DLR对我来说是一个相对较新的领域

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Threading;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            dynamic DefaultConfig = new
            {
                BlacklistedDomains = new string[] { "domain1.com" },
                ExternalConfigFile = "blacklist.txt",
                UseSockets = new[] { 
                    new { IP = "127.0.0.1", Port = "80" }, 
                    new { IP = "127.0.0.2", Port = "8080" } 
                }
            };
            dynamic UserSpecifiedConfig = new
            {
                BlacklistedDomains = new string[] { "example1.com" },
                ExternalConfigFile = "C:''my_blacklist.txt"
            };
            var result = Merge(UserSpecifiedConfig, DefaultConfig);
            // result should now be equal to: 
            var result_equal = new
            {
                BlacklistedDomains = new string[] { "domain1.com", "example1.com" },
                ExternalConfigFile = "C:''my_blacklist.txt",
                UseSockets = new[] {         
                    new { IP = "127.0.0.1", Port = "80"},         
                    new { IP = "127.0.0.2", Port = "8080" }     
                }
            };
            Debug.Assert(result.Equals(result_equal));
        }
        /// <summary>
        /// Merge the properties of two dynamic objects, taking the LHS as primary
        /// </summary>
        /// <param name="lhs"></param>
        /// <param name="rhs"></param>
        /// <returns></returns>
        static dynamic Merge(dynamic lhs, dynamic rhs)
        {
            // get the anonymous type definitions
            Type lhsType = ((Type)((dynamic)lhs).GetType());
            Type rhsType = ((Type)((dynamic)rhs).GetType());
            object result = new { };
            var resProps = new Dictionary<string, PropertyInfo>();
            var resVals = new Dictionary<string, object>();
            var lProps = lhsType.GetProperties().ToDictionary<PropertyInfo, string>(prop => prop.Name);
            var rProps = rhsType.GetProperties().ToDictionary<PropertyInfo, string>(prop => prop.Name); 

            foreach (string leftPropKey in lProps.Keys)
            {
                var lPropInfo = lProps[leftPropKey];
                resProps.Add(leftPropKey, lPropInfo);
                var lhsVal = Convert.ChangeType(lPropInfo.GetValue(lhs, null), lPropInfo.PropertyType);
                if (rProps.ContainsKey(leftPropKey))
                {
                    PropertyInfo rPropInfo;
                    rPropInfo = rProps[leftPropKey];
                    var rhsVal = Convert.ChangeType(rPropInfo.GetValue(rhs, null), rPropInfo.PropertyType);
                    object setVal = null;
                    if (lPropInfo.PropertyType.IsAnonymousType())
                    {
                        setVal = Merge(lhsVal, rhsVal);
                    }
                    else if (lPropInfo.PropertyType.IsArray)
                    {
                        var bound = ((Array) lhsVal).Length + ((Array) rhsVal).Length;
                        var cons = lPropInfo.PropertyType.GetConstructor(new Type[] { typeof(int) });
                        dynamic newArray = cons.Invoke(new object[] { bound });
                        //newArray = ((Array)lhsVal).Clone();
                        int i=0;
                        while (i < ((Array)lhsVal).Length)
                        {
                            newArray[i] = lhsVal[i];
                            i++;
                        }
                        while (i < bound)
                        {
                            newArray[i] = rhsVal[i - ((Array)lhsVal).Length];
                            i++;
                        }
                        setVal = newArray;
                    }
                    else
                    {
                        setVal = lhsVal == null ? rhsVal : lhsVal;
                    }
                    resVals.Add(leftPropKey, setVal);
                }
                else 
                {
                    resVals.Add(leftPropKey, lhsVal);
                }
            }
            foreach (string rightPropKey in rProps.Keys)
            {
                if (lProps.ContainsKey(rightPropKey) == false)
                {
                    PropertyInfo rPropInfo;
                    rPropInfo = rProps[rightPropKey];
                    var rhsVal = rPropInfo.GetValue(rhs, null);
                    resProps.Add(rightPropKey, rPropInfo);
                    resVals.Add(rightPropKey, rhsVal);
                }
            }
            Type resType = TypeExtensions.ToType(result.GetType(), resProps);
            result = Activator.CreateInstance(resType);
            foreach (string key in resVals.Keys)
            {
                var resInfo = resType.GetProperty(key);
                resInfo.SetValue(result, resVals[key], null);
            }
            return result;
        }
    }
}
public static class TypeExtensions
{
    public static Type ToType(Type type, Dictionary<string, PropertyInfo> properties)
    {
        AppDomain myDomain = Thread.GetDomain();
        Assembly asm = type.Assembly;
        AssemblyBuilder assemblyBuilder = 
            myDomain.DefineDynamicAssembly(
            asm.GetName(), 
            AssemblyBuilderAccess.Run
        );
        ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(type.Module.Name);
        TypeBuilder typeBuilder = moduleBuilder.DefineType(type.Name,TypeAttributes.Public);
        foreach (string key in properties.Keys)
        {
            string propertyName = key;
            Type propertyType = properties[key].PropertyType;
            FieldBuilder fieldBuilder = typeBuilder.DefineField(
                "_" + propertyName,
                propertyType,
                FieldAttributes.Private
            );
            PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(
                propertyName,
                PropertyAttributes.HasDefault,
                propertyType,
                new Type[] { }
            );
            // First, we'll define the behavior of the "get" acessor for the property as a method.
            MethodBuilder getMethodBuilder = typeBuilder.DefineMethod(
                "Get" + propertyName,
                MethodAttributes.Public,
                propertyType,
                new Type[] { }
            );
            ILGenerator getMethodIL = getMethodBuilder.GetILGenerator();
            getMethodIL.Emit(OpCodes.Ldarg_0);
            getMethodIL.Emit(OpCodes.Ldfld, fieldBuilder);
            getMethodIL.Emit(OpCodes.Ret);
            // Now, we'll define the behavior of the "set" accessor for the property.
            MethodBuilder setMethodBuilder = typeBuilder.DefineMethod(
                "Set" + propertyName,
                MethodAttributes.Public,
                null,
                new Type[] { propertyType }
            );
            ILGenerator custNameSetIL = setMethodBuilder.GetILGenerator();
            custNameSetIL.Emit(OpCodes.Ldarg_0);
            custNameSetIL.Emit(OpCodes.Ldarg_1);
            custNameSetIL.Emit(OpCodes.Stfld, fieldBuilder);
            custNameSetIL.Emit(OpCodes.Ret);
            // Last, we must map the two methods created above to our PropertyBuilder to 
            // their corresponding behaviors, "get" and "set" respectively. 
            propertyBuilder.SetGetMethod(getMethodBuilder);
            propertyBuilder.SetSetMethod(setMethodBuilder);
        }
        //MethodBuilder toStringMethodBuilder = typeBuilder.DefineMethod(
        //    "ToString",
        //    MethodAttributes.Public,
        //    typeof(string),
        //    new Type[] { }
        //);
        return typeBuilder.CreateType();
    }
    public static Boolean IsAnonymousType(this Type type)
    {
        Boolean hasCompilerGeneratedAttribute = type.GetCustomAttributes(
            typeof(CompilerGeneratedAttribute), false).Count() > 0;
        Boolean nameContainsAnonymousType =
            type.FullName.Contains("AnonymousType");
        Boolean isAnonymousType = hasCompilerGeneratedAttribute && nameContainsAnonymousType;
        return isAnonymousType;
    }
}

这对我来说很有效,但我相信它会得到一些爱和关注,看起来会更好。它不包括您的类型检查,但添加它将是相当琐碎的。因此,虽然这不是一个完美的答案,但我希望它能让你更接近解决方案。

对DynamicIntoExpando(…)的后续调用将继续向现有的Source结构追加和覆盖新值和现有值。您可以根据需要多次调用它。函数MergeDynamic()说明了如何将两个动力学合并为一个ExpandoObject。

代码基本上对动态值进行迭代,检查类型,并适当地递归合并到任何深度。

出于我自己的目的,我把它封装在一个助手类中。

using System.Dynamic; // For ExpandoObject
...
public static class DynamicHelper
{
    // We expect inputs to be of type IDictionary
    public static ExpandoObject MergeDynamic(dynamic Source, dynamic Additional)
    {
        ExpandoObject Result = new ExpandoObject();
        // First copy 'source' to Result
        DynamicIntoExpando(Result, Source);
        // Then copy additional fields, boldy overwriting the source as needed
        DynamicIntoExpando(Result, Additional);
        // Done
        return Result;
    }
    public static void DynamicIntoExpando(ExpandoObject Result, dynamic Source, string Key = null)
    {
        // Cast it for ease of use.
        var R = Result as IDictionary<string, dynamic>;
        if (Source is IDictionary<string, dynamic>)
        {
            var S = Source as IDictionary<string, dynamic>;
            ExpandoObject NewDict = new ExpandoObject();
            if (Key == null)
            {
                NewDict = Result;
            }
            else if (R.ContainsKey(Key))
            {
                // Already exists, overwrite
                NewDict = R[Key];
            }
            var ND = NewDict as IDictionary<string, dynamic>;
            foreach (string key in S.Keys)
            {
                ExpandoObject NewDictEntry = new ExpandoObject();
                var NDE = NewDictEntry as IDictionary<string, dynamic>;
                if (ND.ContainsKey(key))
                {
                    NDE[key] = ND[key];
                }
                else if (R.ContainsKey(key))
                {
                    NDE[key] = R[key];
                }
                DynamicIntoExpando(NewDictEntry, S[key], key);
                if(!R.ContainsKey(key)) {
                    ND[key] = ((IDictionary<string, dynamic>)NewDictEntry)[key];
                }
            }
            if (Key == null)
            {
                R = NewDict;
            }
            else if (!R.ContainsKey(Key))
            {
                R.Add(Key, NewDict);
            }
        }
        else if (Source is IList<dynamic>)
        {
            var S = Source as IList<dynamic>;
            List<ExpandoObject> NewList = new List<ExpandoObject>();
            if (Key != null && R.ContainsKey(Key))
            {
                // Already exists, overwrite
                NewList = (List<ExpandoObject>)R[Key];
            }
            foreach (dynamic D in S)
            {
                ExpandoObject ListEntry = new ExpandoObject();
                DynamicIntoExpando(ListEntry, D);
                //  in this case we have to compare the ListEntry to existing entries and on
                NewList.Add(ListEntry);
            }
            if (Key != null && !R.ContainsKey(Key))
            {
                R[Key] = NewList.Distinct().ToList(); 
            }
        }
        else
        {
            R[Key] = Source;
        }
    }
}