Copy two identical object with different namespaces (recursive reflection)

后端 未结 7 1486
我寻月下人不归
我寻月下人不归 2021-01-06 19:22

I\'m working in c# with several workspaces that have one specific class which his always the same in each workspace. I would like to be able have a copy of this class to be

7条回答
  •  星月不相逢
    2021-01-06 20:13

    I got it solved , just to let you know how I did it : This solution is sot perfect because it handle only 1 dimensions array not more.

    public static Object CopyObject(Object from , Object to)
    {
        try
        {
            Type fromType = from.GetType();
            Type toType = to.GetType();
    
            PropertyInfo[] fromProps = fromType.GetProperties();
            PropertyInfo[] toProps = toType.GetProperties();
    
            for (int i = 0; i < fromProps.Length; i++)
            {
                PropertyInfo fromProp = fromProps[i];
                PropertyInfo toProp = toType.GetProperty(fromProp.Name);
                if (toProp != null)
                {
                    if (toProp.PropertyType.Module.ScopeName != "CommonLanguageRuntimeLibrary")
                    {
                        if (!toProp.PropertyType.IsArray)
                        {
                            ConstructorInfo ci = toProp.PropertyType.GetConstructor(new Type[0]);
                            if (ci != null)
                            {
                                toProp.SetValue(to, ci.Invoke(null), null);
                                toProp.SetValue(to, gestionRefelexion.CopyObject(fromProp.GetValue(from, null), toProp.GetValue(to, null)), null);
                            }
                        }
                        else
                        {
                            Type typeToArray = toProp.PropertyType.GetElementType();
                            Array fromArray = fromProp.GetValue(from, null) as Array;
                            toProp.SetValue(to, copyArray(fromArray, typeToArray), null);
                        }
                    }
                    else
                    {
                        toProp.SetValue(to, fromProp.GetValue(from, null), null);
                    }
                }
            }
        }
        catch (Exception ex)
        {
        }
        return to;
    }
    
    public static Array copyArray(Array from, Type toType)
    {
        Array toArray =null;
        if (from != null)
        {
            toArray= Array.CreateInstance(toType, from.Length);
    
            for (int i = 0; i < from.Length; i++)
            {
                ConstructorInfo ci = toType.GetConstructor(new Type[0]);
                if (ci != null)
                {
                    toArray.SetValue(ci.Invoke(null), i);
                    toArray.SetValue(gestionRefelexion.CopyObject(from.GetValue(i), toArray.GetValue(i)), i);
                }
            }
        }
        return toArray;
    }
    

    Hope this can help some people. Thanks for helping everyone. Cheers

提交回复
热议问题