示例#1
0
            /// <summary>Merge two different object instances into a single object which is a super-set of the properties of both objects.</summary>
            public static object MergeTypes(object values1, object values2)
            {
                // create a name from the names of both Types
                string name1 = String.Format("{0}_{1}", values1.GetType(), values2.GetType());
                string name2 = String.Format("{0}_{1}", values2.GetType(), values1.GetType());

                object newValues = TypeMerger.CreateInstance(name1, values1, values2);

                if (newValues != null)
                {
                    return(newValues);
                }

                newValues = TypeMerger.CreateInstance(name2, values2, values1);

                if (newValues != null)
                {
                    return(newValues);
                }

                // lock for thread safe writing
                lock (_syncLock)
                {
                    // now that we're inside the lock - check one more time
                    newValues = TypeMerger.CreateInstance(name1, values1, values2);

                    if (newValues != null)
                    {
                        return(newValues);
                    }

                    // merge list of PropertyDescriptors for both objects
                    PropertyDescriptor[] pdc = TypeMerger.GetProperties(values1, values2);

                    // make sure static properties are properly initialized
                    TypeMerger.InitializeAssembly();

                    // create the type definition
                    Type newType = TypeMerger.CreateType(name1, pdc);

                    // add it to the cache
                    TypeMerger.kAnonymousTypes.Add(name1, newType);

                    // return an instance of the new Type
                    return(TypeMerger.CreateInstance(name1, values1, values2));
                }
            }