コード例 #1
0
    public static void CopyItem <T>(BigClass source, T target)
    {
        // Need a way to rename the backing-field name to the property Name ("<A>k__BackingField" => "A")
        Func <string, string> renameBackingField = key => new string(key.Skip(1).Take(key.IndexOf('>') - 1).ToArray());

        // Get public source properties (change BindingFlags if you need to copy private memebers as well)
        var sourceProperties = source.GetType().GetProperties().ToDictionary(item => item.Name);
        // Get "missing" property setter's backing field
        var targetFields = typeof(T).GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetField).ToDictionary(item => renameBackingField(item.Name));

        // Copy properties where target name matches the source property name
        foreach (var sourceProperty in sourceProperties)
        {
            if (targetFields.ContainsKey(sourceProperty.Key) == false)
            {
                continue;     // No match. skip
            }
            var sourceValue = sourceProperty.Value.GetValue(source);
            targetFields[sourceProperty.Key].SetValue(target, sourceValue);
        }
    }
コード例 #2
0
        static void Main(string[] args)
        {
            //var user = new User();
            //user.MyProperty.Add(new SmallClass
            //{
            //    MyProperty = 1
            //});

            //var user1 = new User();
            //user1.MyProperty.Add(new SmallClass
            //{
            //    MyProperty = 111
            //});
            //user1.MyProperty.Add(new SmallClass
            //{
            //    MyProperty = 222
            //});

            var bigClass = new BigClass
            {
                One   = 1,
                Two   = 1.0M,
                Three = "1",
                Four  = 0,
                Five  = 2,
            };

            var smallClass = new SmallClass
            {
                MyProperty = 1
            };

            var anonObject = new AnonymousObjectsExample();
            var dtoObject1 = new DataTransferObject
            {
                Value = bigClass.Two,
                Type  = bigClass.GetType(),
            };
            var dtoObject2 = new DataTransferObject
            {
                Value = smallClass.MyProperty,
                Type  = smallClass.GetType(),
            };

            // Огромные вычисления :)
            var anonObject3 = new
            {
                Value = bigClass.Two,
                Type  = bigClass.GetType(),
            };
            var dtoObject3ByAnonObject = new DataTransferObject
            {
                Value = anonObject3.Value,
                Type  = anonObject3.Type,
            };

            Console.WriteLine(anonObject3.GetType());
            Console.WriteLine(dtoObject3ByAnonObject.GetType());

            anonObject.Show(dtoObject3ByAnonObject);
            anonObject.Show(dtoObject1);
            anonObject.Show(dtoObject2);

            var listWithValues = new List <int>
            {
                0,
                1,
                2,
                3,
                0,
                4,
                0,
                5,
                0,
            };

            var resultValue1 = new List <ResultObject>();
            var resultValue2 = new List <ResultObject>();
            var resultValue3 = new List <ResultObject>();

            foreach (var item in listWithValues)
            {
                var newTempObject = new ResultObject
                {
                    Value  = item,
                    Result = CheckUp(item)
                };

                resultValue1.Add(newTempObject);
            }