Beispiel #1
0
 protected override object GetFilterValues()
 {
     return(TypeMerger.Merge(new
     {
         search = Filter.Search,
     }, base.GetFilterValues()));
 }
Beispiel #2
0
        public void Test_Basic_Merge_Types()
        {
            var obj1 = new { Property1 = "1", Property2 = "2", Property3 = "3", Property4 = "4", Property5 = "5", Property6 = "6", Property7 = "7", Property8 = "8", Property9 = "9", Property10 = "10" };
            var obj2 = new { Property11 = "11", Property12 = "12", Property13 = "13", Property14 = "14", Property15 = "15", Property16 = "16", Property17 = "17", Property18 = "18", Property19 = "19", Property20 = "20" };

            var result = TypeMerger.Merge(obj1, obj2);

            result.GetType().GetProperties().Length.Should().Be(20);

            result.GetType().GetProperty("Property1").GetValue(result).Should().Be("1");
            result.GetType().GetProperty("Property2").GetValue(result).Should().Be("2");
            result.GetType().GetProperty("Property3").GetValue(result).Should().Be("3");
            result.GetType().GetProperty("Property4").GetValue(result).Should().Be("4");
            result.GetType().GetProperty("Property5").GetValue(result).Should().Be("5");
            result.GetType().GetProperty("Property6").GetValue(result).Should().Be("6");
            result.GetType().GetProperty("Property7").GetValue(result).Should().Be("7");
            result.GetType().GetProperty("Property8").GetValue(result).Should().Be("8");
            result.GetType().GetProperty("Property9").GetValue(result).Should().Be("9");
            result.GetType().GetProperty("Property10").GetValue(result).Should().Be("10");
            result.GetType().GetProperty("Property11").GetValue(result).Should().Be("11");
            result.GetType().GetProperty("Property12").GetValue(result).Should().Be("12");
            result.GetType().GetProperty("Property13").GetValue(result).Should().Be("13");
            result.GetType().GetProperty("Property14").GetValue(result).Should().Be("14");
            result.GetType().GetProperty("Property15").GetValue(result).Should().Be("15");
            result.GetType().GetProperty("Property16").GetValue(result).Should().Be("16");
            result.GetType().GetProperty("Property17").GetValue(result).Should().Be("17");
            result.GetType().GetProperty("Property18").GetValue(result).Should().Be("18");
            result.GetType().GetProperty("Property19").GetValue(result).Should().Be("19");
            result.GetType().GetProperty("Property20").GetValue(result).Should().Be("20");
        }
Beispiel #3
0
        internal static object[] ClassifyObjects(object[] args)
        {
            List <object> objIndex = new List <object>();
            object        objName  = null;

            foreach (var arg in args)
            {
                if (arg != null)
                {
                    if (Formatter.IsAnonymousType(arg.GetType()))
                    {
                        if (objName == null)
                        {
                            objName = arg;
                        }
                        else
                        {
                            objName = TypeMerger.MergeTypes(objName, arg);
                        }
                    }
                    else
                    {
                        objIndex.Add(arg);
                    }
                }
            }

            if (objName != null)
            {
                objIndex.Add(objName);
            }

            return(objIndex.ToArray());
        }
Beispiel #4
0
 protected override object GetFilterValues()
 {
     return(TypeMerger.Merge(new
     {
         page = Page == 1 ? (int?)null : Page,
     }, base.GetFilterValues()));
 }
Beispiel #5
0
            /// <summary>Instantiates an instance of an existing Type from cache.</summary>
            private static object CreateInstance(string name, object values1, object values2)
            {
                object newValues = null;

                // merge all values together into an array
                object[] allValues = TypeMerger.MergeValues(values1, values2);

                // check to see if type exists
                if (TypeMerger.kAnonymousTypes.ContainsKey(name))
                {
                    // get type
                    Type type = TypeMerger.kAnonymousTypes[name];

                    // make sure it isn't null for some reason
                    if (type != null)
                    { // create a new instance
                        newValues = Activator.CreateInstance(type, allValues);
                    }
                    else
                    { // remove null type entry
                        lock (_syncLock)
                        {
                            TypeMerger.kAnonymousTypes.Remove(name);
                        }
                    }
                }

                // return values (if any)
                return(newValues);
            }
Beispiel #6
0
        public void Test_Type_Creation_from_Concrete_Classes()
        {
            var class1 = new TestClass1 {
                Name        = "Foo",
                Description = "Test Class Instance",
                Number      = 10,
                SubClass    = new TestSubClass1 {
                    Internal = "Inside"
                }
            };

            var class2 = new TestClass2 {
                FullName    = "Test Class 2",
                FullAddress = "123 Main St.",
                Total       = 28
            };

            var result = TypeMerger.Ignore(() => class1.Name)
                         .Ignore(() => class2.Total)
                         .Merge(class1, class2);

            result.GetType().GetProperties().Length.Should().Be(5);

            result.GetType().GetProperty("SubClass").PropertyType.Should().Be(typeof(TestSubClass1));
            result.GetType().GetProperty("SubClass").GetValue(result)
            .GetType().GetProperty("Internal")
            .GetValue(result.GetType().GetProperty("SubClass")
                      .GetValue(result)).Should().Be(class1.SubClass.Internal);
        }
Beispiel #7
0
 protected override object GetFilterValues()
 {
     return(TypeMerger.Merge(new
     {
         title = Filter.Title,
         rating = Filter.Rating,
         approved = Filter.Approved
     }, base.GetFilterValues()));
 }
Beispiel #8
0
 protected override object GetFilterValues()
 {
     return(TypeMerger.Merge(new
     {
         orderCode = Filter.OrderCode,
         trackingCode = Filter.TrackingCode,
         status = Filter.Status,
         search = Filter.Search,
     }, base.GetFilterValues()));
 }
Beispiel #9
0
 public object GetRequestValues(object values = null)
 {
     if (values != null)
     {
         return(TypeMerger.Merge(GetFilterValues(), values));
     }
     else
     {
         return(GetFilterValues());
     }
 }
Beispiel #10
0
        public void Test_Use_Method_to_Handle_Name_Collision_Priority()
        {
            var obj1 = new { Property1 = "value1", Property2 = "2" };
            var obj2 = new { Property1 = "value2", Property3 = "3" };

            var result1 = TypeMerger.Use(() => obj2.Property1)
                          .Merge(obj1, obj2);

            result1.GetType().GetProperties().Length.Should().Be(3);
            result1.GetType().GetProperty("Property1").GetValue(result1).Should().Be("value2");
        }
Beispiel #11
0
 protected override object GetFilterValues()
 {
     return(TypeMerger.Merge(new
     {
         storeCategory = Filter.StoreCategory,
         storeSetup = Filter.StoreSetup,
         storeRegion = Filter.StoreRegion,
         storePlace = Filter.StorePlace,
         search = Filter.Search,
     }, base.GetFilterValues()));
 }
 protected override object GetFilterValues()
 {
     return(TypeMerger.Merge(new
     {
         status = Filter.Status,
         processor = Filter.Processor,
         type = Filter.Type,
         mode = Filter.Mode,
         search = Filter.Search,
     }, base.GetFilterValues()));
 }
Beispiel #13
0
        public void Test_Derived_Class_with_Ignored_Base_Class_Property()
        {
            var obj1 = new DerivedClass {
                Name = "foo"
            };

            var obj2 = new { Value = 123 };

            var result = TypeMerger.Ignore(() => obj1.Name).Merge(obj1, obj2);

            result.GetType().GetProperties().Length.Should().Be(1);
        }
Beispiel #14
0
 protected override object GetFilterValues()
 {
     return(TypeMerger.Merge(new
     {
         stock = Filter.Stock,
         search = Filter.Search,
         minPrice = Filter.MinPrice,
         maxPrice = Filter.MaxPrice,
         rating = Filter.Rating,
         sort = Filter.Sort,
         discount = Filter.Discount
     }, base.GetFilterValues()));
 }
Beispiel #15
0
        public void Test_Ignore_and_Use_Methods_used_in_Single_Merge_Policy()
        {
            var obj1 = new { Property1 = "value1", Property2 = "2" };
            var obj2 = new { Property1 = "value2", Property3 = "3" };

            var result = TypeMerger.Use(() => obj2.Property1)
                         .Ignore(() => obj2.Property3)
                         .Merge(obj1, obj2);

            result.GetType().GetProperties().Length.Should().Be(2);
            result.GetType().GetProperty("Property1").GetValue(result).Should().Be(obj2.Property1);
            result.GetType().GetProperty("Property2").GetValue(result).Should().Be(obj1.Property2);
        }
Beispiel #16
0
        public void Merge_Types_with_Ignore_Policy()
        {
            var obj1 = new { Property1 = "value1", Property2 = "value1" };
            var obj2 = new { Property1 = "value2", Property4 = "value4" };

            var result = TypeMerger.Ignore(() => obj1.Property1)
                         .Ignore(() => obj2.Property4)
                         .Merge(obj1, obj2);

            result.GetType().GetProperties().Length.Should().Be(2);
            result.GetType().GetProperty("Property1").GetValue(result).Should().Be("value2");
            result.GetType().GetProperty("Property2").Should().NotBeNull();
        }
Beispiel #17
0
        public void Merge_Types_with_Ignore_Policy()
        {
            var obj1 = new { Property1 = "values1", Property2 = "values1" };
            var obj2 = new { Property1 = "values2", Property4 = "values4" };

            var result = TypeMerger.Ignore(() => obj1.Property1)
                         .Ignore(() => obj2.Property4)
                         .Merge(obj1, obj2);

            Assert.Equal(2, result.GetType().GetProperties().Length);

            Assert.Equal("values2", result.GetType().GetProperty("Property1").GetValue(result));

            Assert.NotNull(result.GetType().GetProperty("Property2"));
        }
Beispiel #18
0
        public void Merge_Types_with_Name_Collision()
        {
            var obj1 = new { Property1 = "value1", Property2 = "2" };
            var obj2 = new { Property1 = "value2", Property3 = "3" };

            var result1 = TypeMerger.Merge(obj1, obj2);

            Assert.Equal(3, result1.GetType().GetProperties().Length);
            Assert.Equal("value1", result1.GetType().GetProperty("Property1").GetValue(result1));

            var result2 = TypeMerger.Merge(obj2, obj1);

            Assert.Equal(3, result2.GetType().GetProperties().Length);
            Assert.Equal("value2", result2.GetType().GetProperty("Property1").GetValue(result2));
        }
Beispiel #19
0
        public void MergeObject_MergesTwoObjects_IntoANewObject()
        {
            var object1 = new { Id = 1, Name = "Object1", DateCreated = new DateTime(2001, 1, 1) };
            var object2 = new { Id = 3, Description = "Object2 is richer", Money = 12345.0M };

            var mergedObject = TypeMerger.MergeTypes(object1, object2);

            var dictionary = mergedObject.ToDictionary();

            Assert.Equal("Object1", dictionary["Name"]);
            Assert.Equal("Object2 is richer", dictionary["Description"]);
            Assert.Equal(12345.0M, dictionary["Money"]);
            Assert.Equal(new DateTime(2001, 1, 1), dictionary["DateCreated"]);
            Assert.Equal(1, dictionary["Id"]);
        }
Beispiel #20
0
        public void Test_Multiple_Type_Creation_from_Same_Anonymous_Types_Sources()
        {
            var obj1 = new { Property1 = "value1", Property2 = "2" };
            var obj2 = new { Property1 = "value2", Property3 = "3" };

            var result1 = TypeMerger.Merge(obj1, obj2);

            result1.GetType().GetProperties().Length.Should().Be(3);
            result1.GetType().GetProperty("Property1").GetValue(result1).Should().Be("value1");

            var result2 = TypeMerger.Ignore(() => obj1.Property2)
                          .Merge(obj1, obj2);

            result2.GetType().GetProperties().Length.Should().Be(2);
            result2.GetType().GetProperty("Property3").GetValue(result2).Should().Be("3");
        }
Beispiel #21
0
        public void Test_Class_with_Built_in_Types()
        {
            var obj1 = new { Property1 = "value1", Property2 = "2" };
            var obj2 = new AllBuiltInTypes {
                ByteType     = Byte.MaxValue,
                SByteType    = SByte.MaxValue,
                Int32Type    = Int32.MaxValue,
                UInt32Type   = UInt32.MaxValue,
                Int16Type    = Int16.MaxValue,
                UInt16Type   = UInt16.MaxValue,
                Int64Type    = Int64.MaxValue,
                UInt64Type   = UInt64.MaxValue,
                SingleType   = Single.MaxValue,
                DoubleType   = Double.MaxValue,
                DecimalType  = 300.5m,
                BooleanType  = false,
                CharType     = '\x0058',
                ObjectType   = new { Test = 1 },
                StringType   = "foo",
                DateTimeType = DateTime.Now,
                EnumType     = TestEnum.Val1
            };

            var result1 = TypeMerger.Merge(obj1, obj2);

            result1.GetType().GetProperties().Length.Should().Be(19);

            result1.GetType().GetProperty("Property1").GetValue(result1).Should().Be(obj1.Property1);
            result1.GetType().GetProperty("Property2").GetValue(result1).Should().Be(obj1.Property2);
            result1.GetType().GetProperty("ByteType").GetValue(result1).Should().Be(obj2.ByteType);
            result1.GetType().GetProperty("SByteType").GetValue(result1).Should().Be(obj2.SByteType);
            result1.GetType().GetProperty("Int32Type").GetValue(result1).Should().Be(obj2.Int32Type);
            result1.GetType().GetProperty("UInt32Type").GetValue(result1).Should().Be(obj2.UInt32Type);
            result1.GetType().GetProperty("Int16Type").GetValue(result1).Should().Be(obj2.Int16Type);
            result1.GetType().GetProperty("UInt16Type").GetValue(result1).Should().Be(obj2.UInt16Type);
            result1.GetType().GetProperty("Int64Type").GetValue(result1).Should().Be(obj2.Int64Type);
            result1.GetType().GetProperty("UInt64Type").GetValue(result1).Should().Be(obj2.UInt64Type);
            result1.GetType().GetProperty("SingleType").GetValue(result1).Should().Be(obj2.SingleType);
            result1.GetType().GetProperty("DoubleType").GetValue(result1).Should().Be(obj2.DoubleType);
            result1.GetType().GetProperty("DecimalType").GetValue(result1).Should().Be(obj2.DecimalType);
            result1.GetType().GetProperty("BooleanType").GetValue(result1).Should().Be(obj2.BooleanType);
            result1.GetType().GetProperty("CharType").GetValue(result1).Should().Be(obj2.CharType);
            result1.GetType().GetProperty("ObjectType").GetValue(result1).Should().Be(obj2.ObjectType);
            result1.GetType().GetProperty("SingleType").GetValue(result1).Should().Be(obj2.SingleType);
            result1.GetType().GetProperty("DateTimeType").GetValue(result1).Should().Be(obj2.DateTimeType);
            result1.GetType().GetProperty("EnumType").GetValue(result1).Should().Be(TestEnum.Val1);
        }
Beispiel #22
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));
                }
            }
Beispiel #23
0
        public void Merge_Types_with_Name_Collision()
        {
            var obj1 = new { Property1 = "value1", Property2 = "2" };
            var obj2 = new { Property1 = "value2", Property3 = "3" };

            var result1 = TypeMerger.Merge(obj1, obj2);

            result1.GetType().GetProperties().Length.Should().Be(3);
            result1.GetType().GetProperty("Property1").GetValue(result1).Should().Be("value1");
            result1.GetType().GetProperty("Property3").GetValue(result1).Should().Be("3");

            var result2 = TypeMerger.Merge(obj2, obj1);

            result2.GetType().GetProperties().Length.Should().Be(3);
            result2.GetType().GetProperty("Property1").GetValue(result2).Should().Be("value2");
            result2.GetType().GetProperty("Property3").GetValue(result2).Should().Be("3");
        }
Beispiel #24
0
        public async Task <CommonResponse> UpdateProfilePrivacy(string QueryUserID, UserProfilePrivacy updatedPrivacy)
        {
            var oldPrivacy = GetProfilePrivacy(QueryUserID);

            if (oldPrivacy != null)
            {
                var merged = TypeMerger.MergeProperties(oldPrivacy, updatedPrivacy, "Userid");
                if (await _users.UpdateProfilePrivacy(merged))
                {
                    return new CommonResponse {
                               StatusCode = 0
                    }
                }
                ;
            }

            return(new CommonResponse {
                StatusCode = -1
            });
        }
Beispiel #25
0
        public CommonResponse GetProfile(string QueryUserID, string ClaimsUserID)
        {
            var user = _users.GetProfile(QueryUserID);

            if (user == null)
            {
                return new CommonResponse {
                           StatusCode = -1
                }
            }
            ;
            else
            {
                var applied = ApplyPrivacyToUserProfile(user, _users.GetProfilePrivacy(user.Userid), QueryUserID == ClaimsUserID);

                return(TypeMerger.MergeProperties(new GetprofileResponse {
                    StatusCode = 0
                }, applied));
            }
        }
Beispiel #26
0
            /// <summary>Create a new Type definition from the list of PropertyDescriptors.</summary>
            private static Type CreateType(string name, PropertyDescriptor[] pdc)
            {
                // create TypeBuilder
                TypeBuilder typeBuilder = TypeMerger.CreateTypeBuilder(name);

                // get list of types for ctor definition
                Type[] types = TypeMerger.GetTypes(pdc);

                // create priate fields for use w/in the ctor body and properties
                FieldBuilder[] fields = TypeMerger.BuildFields(typeBuilder, pdc);

                // define / emit the Ctor
                TypeMerger.BuildCtor(typeBuilder, fields, types);

                // define / emit the properties
                TypeMerger.BuildProperties(typeBuilder, fields);

                // return Type definition
                return(typeBuilder.CreateType());
            }
Beispiel #27
0
        public async Task <CommonResponse> ModifyProfile(string UserID, UserProfile NewProfile)
        {
            var user = _users.GetProfile(UserID);

            if (user != null)
            {
                var merged = TypeMerger.MergeProperties(user, NewProfile, "Userid");
                if (await _users.UpdateUserProfile(merged))
                {
                    return new CommonResponse {
                               StatusCode = 0
                    }
                }
                ;
                return(new CommonResponse {
                    StatusCode = -3
                });
            }
            return(new CommonResponse {
                StatusCode = -2
            });
        }
Beispiel #28
0
 public PlaylistMusic ToPlaylistMusic()
 {
     return(TypeMerger.MergeProperties(new PlaylistMusic(), this) as PlaylistMusic);
 }
Beispiel #29
0
 public object MergeTypes(object values1, object values2)
 {
     return(TypeMerger.MergeTypes(values1, values2, this));
 }
Beispiel #30
0
            /// <summary>.</summary>
            public static object MergeTypes(object values1, object values2, TypeMergerPolicy policy)
            {
                TypeMerger.kTypeMergerPolicy = policy;

                return(TypeMerger.MergeTypes(values1, values2));
            }