Cilador.Fody.DtoProjector.DtoProjectorWeave.Weave C# (CSharp) Method

Weave() public method

Projects a DTO type as a nested type inside of the target type.
public Weave ( IWeavingContext weavingContext, Mono.Cecil.TypeDefinition target, CustomAttribute weaveAttribute ) : void
weavingContext IWeavingContext Weaving context within which the weave is running.
target Mono.Cecil.TypeDefinition Target type.
weaveAttribute Mono.Cecil.CustomAttribute Attribute that decorated the target type.
return void
        public void Weave(IWeavingContext weavingContext, TypeDefinition target, CustomAttribute weaveAttribute)
        {
            TypeDefinition dtoType = new TypeDefinition(
                target.Namespace, "Dto",
                TypeAttributes.Class | TypeAttributes.NestedPublic,
                target.Module.Import(typeof(object)));
            target.NestedTypes.Add(dtoType);

            var dtoMemberAttributeType = target.Module.Import(typeof (DtoMemberAttribute)).Resolve();

            foreach (var property in target.Properties)
            {
                for (var i = property.CustomAttributes.Count - 1; i >= 0; --i)
                {
                    var attribute = property.CustomAttributes[i];
                    if (attribute.AttributeType.Resolve() == dtoMemberAttributeType)
                    {
                        property.CustomAttributes.RemoveAt(i);

                        var field = new FieldDefinition(property.Name, FieldAttributes.Public, property.PropertyType);
                        dtoType.Fields.Add(field);
                    }
                }
            }
        }
DtoProjectorWeave