Esempio n. 1
0
        private static void AddDataTransferObject(TypeDefinition target)
        {
            Contract.Requires(target != null);

            var typeModule = target.Module;
            var dtoType = new TypeDefinition(string.Empty, "Dto", TypeAttributes.NestedPublic | TypeAttributes.Class, typeModule.Import(typeof(object)));
            target.NestedTypes.Add(dtoType);
            dtoType.AddPublicConstructor(
                ilProcessor =>
                {
                    ilProcessor.Append(Instruction.Create(OpCodes.Ldarg_0));
                    ilProcessor.Append(Instruction.Create(OpCodes.Call, typeModule.ImportConstructor(typeof(object))));
                    ilProcessor.Append(Instruction.Create(OpCodes.Ret));
                });

            var encapsulatedAttributeType = typeModule.Import(typeof(EncapsulatedAttribute)).Resolve();
            foreach (var property in target.Properties)
            {
                var encapsulatedAttribute = property.CustomAttributes.SingleOrDefault(
                    customAttribute => customAttribute.AttributeType.Resolve() == encapsulatedAttributeType);

                if (encapsulatedAttribute != null)
                {
                    dtoType.AddAutoProperty(property.Name, property.PropertyType);
                    property.CustomAttributes.Remove(encapsulatedAttribute);
                }
            }
        }