Exemplo n.º 1
0
        private bool TryCopy(PropertyDefinition property, out IEnumerable <Instruction> instructions)
        {
            if (property.AnyAttribute(IgnoreDuringDeepCopyAttribute))
            {
                property.CustomAttributes.Remove(property.SingleAttribute(IgnoreDuringDeepCopyAttribute));
                instructions = null;
                return(false);
            }

            if (property.GetMethod == null ||
                property.SetMethod == null && property.GetBackingField() == null)
            {
                instructions = null;
                return(false);
            }

            if (property.AnyAttribute(DeepCopyByReferenceAttribute))
            {
                property.CustomAttributes.Remove(property.SingleAttribute(DeepCopyByReferenceAttribute));
                instructions = new[]
                {
                    Instruction.Create(OpCodes.Ldarg_0),
                    Instruction.Create(OpCodes.Ldarg_1),
                    Instruction.Create(OpCodes.Callvirt, property.GetMethod),
                    property.CreateSetInstruction()
                };
                return(true);
            }

            var source = ValueSource.New().Property(property);
            var target = ValueTarget.New().Property(property);

            var list = new List <Instruction>();

            instructions = list;

            if (property.PropertyType.IsArray)
            {
                using (new IfNotNull(list, source))
                    list.AddRange(CopyArray(property));
                return(true);
            }

            instructions = Copy(property.PropertyType, source, target);
            return(true);
        }