Exemple #1
0
        public Property CreateProperty(Modifiers modifier, BuilderType propertyType, string name, PropertySetterCreationOption setterCreationOption = PropertySetterCreationOption.AlwaysCreate)
        {
            var contain = this.GetProperties().Get(name);

            if (contain != null)
            {
                return(new Property(this, contain));
            }

            var createSetter = setterCreationOption == PropertySetterCreationOption.AlwaysCreate;
            var attributes   = MethodAttributes.HideBySig | MethodAttributes.SpecialName;

            if (modifier.HasFlag(Modifiers.Private))
            {
                attributes |= MethodAttributes.Private;
            }
            if (modifier.HasFlag(Modifiers.Static))
            {
                attributes |= MethodAttributes.Static;
            }
            if (modifier.HasFlag(Modifiers.Public))
            {
                attributes |= MethodAttributes.Public;
            }
            if (modifier.HasFlag(Modifiers.Protected))
            {
                attributes |= MethodAttributes.Family;
            }

            var returnType   = this.moduleDefinition.ImportReference(propertyType.typeReference);
            var property     = new PropertyDefinition(name, PropertyAttributes.None, returnType);
            var backingField = this.CreateField(modifier, returnType, $"<{name}>k__BackingField");

            // This should be here to insure that the field does not have these attributes... Is that even possible?
            if (modifier.HasFlag(Modifiers.Overrides))
            {
                attributes |= MethodAttributes.Final | MethodAttributes.Virtual | MethodAttributes.NewSlot;
            }

            property.GetMethod = new MethodDefinition("get_" + name, attributes, returnType);
            this.typeDefinition.Properties.Add(property);
            this.typeDefinition.Methods.Add(property.GetMethod);

            if (createSetter)
            {
                property.SetMethod = new MethodDefinition("set_" + name, attributes, this.moduleDefinition.TypeSystem.Void);
                property.SetMethod.Parameters.Add(new ParameterDefinition("value", ParameterAttributes.None, returnType));
                this.typeDefinition.Methods.Add(property.SetMethod);
            }

            var result = new Property(this, property);

            result.Getter.NewCoder().Load(backingField).Return().Replace();

            if (createSetter)
            {
                result.Setter.NewCoder().SetValue(backingField, CodeBlocks.GetParameter(0)).Return().Replace();
            }

            result.RefreshBackingField();
            return(result);
        }
Exemple #2
0
        public Property CreateProperty(Field field, PropertySetterCreationOption setterCreationOption = PropertySetterCreationOption.AlwaysCreate)
        {
            var name = $"<{field.Name}>_fieldProperty";

            var contain = this.GetProperties().Get(name);

            if (contain != null)
            {
                return(new Property(this, contain));
            }

            var createSetter = setterCreationOption == PropertySetterCreationOption.AlwaysCreate;
            var attributes   = MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.Private;

            if (field.Modifiers.HasFlag(Modifiers.Private))
            {
                attributes |= MethodAttributes.Private;
            }
            if (field.Modifiers.HasFlag(Modifiers.Static))
            {
                attributes |= MethodAttributes.Static;
            }
            if (field.Modifiers.HasFlag(Modifiers.Public))
            {
                attributes |= MethodAttributes.Public;
            }
            if (field.Modifiers.HasFlag(Modifiers.Protected))
            {
                attributes |= MethodAttributes.Family;
            }
            if (field.Modifiers.HasFlag(Modifiers.Overrides))
            {
                attributes |= MethodAttributes.Final | MethodAttributes.Virtual | MethodAttributes.NewSlot;
            }

            var property = new PropertyDefinition(name, PropertyAttributes.None, field.FieldType.typeReference)
            {
                GetMethod = new MethodDefinition("get_" + name, attributes, field.FieldType.typeReference)
            };

            this.typeDefinition.Properties.Add(property);
            this.typeDefinition.Methods.Add(property.GetMethod);

            if (createSetter)
            {
                property.SetMethod = new MethodDefinition("set_" + name, attributes, this.moduleDefinition.TypeSystem.Void);
                property.SetMethod.Parameters.Add(new ParameterDefinition("value", ParameterAttributes.None, field.FieldType.typeReference));
                this.typeDefinition.Methods.Add(property.SetMethod);
            }

            var result = new Property(this, property);

            result.Getter.NewCoder().Load(field).Return().Replace();

            if (createSetter)
            {
                result.Setter.NewCoder().SetValue(field, CodeBlocks.GetParameter(0)).Return().Replace();
            }

            result.RefreshBackingField();
            return(result);
        }
Exemple #3
0
 public Property CreateProperty(Modifiers modifier, Type propertyType, string name, PropertySetterCreationOption setterCreationOption = PropertySetterCreationOption.AlwaysCreate) =>
 this.CreateProperty(modifier, this.Builder.GetType(propertyType), name, setterCreationOption);