public Property CreateProperty(Field field, bool getterOnly = false) { var name = $"<{field.Name}>_fieldProperty"; var contain = this.GetProperties().Get(name); if (contain != null) { return(new Property(this, contain)); } 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.Overrrides)) { attributes |= MethodAttributes.Final | MethodAttributes.Virtual | MethodAttributes.NewSlot; } var property = new PropertyDefinition(name, PropertyAttributes.None, field.FieldType.typeReference); property.GetMethod = new MethodDefinition("get_" + name, attributes, field.FieldType.typeReference); this.typeDefinition.Properties.Add(property); this.typeDefinition.Methods.Add(property.GetMethod); if (!getterOnly) { 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.NewCode().Load(field).Return().Replace(); if (!getterOnly) { result.Setter.NewCode().Assign(field).Set(Crumb.GetParameter(0)).Return().Replace(); } result.RefreshBackingField(); return(result); }
public void AddSetter() { this.propertyDefinition.SetMethod = new MethodDefinition("set_" + this.Name, this.propertyDefinition.GetMethod.Attributes, this.moduleDefinition.TypeSystem.Void); this.propertyDefinition.SetMethod.Parameters.Add(new ParameterDefinition("value", ParameterAttributes.None, this.ReturnType.typeReference)); this.type.typeDefinition.Methods.Add(this.propertyDefinition.SetMethod); this.Setter = new Method(this.type, this.propertyDefinition.SetMethod); this.Setter.NewCode().Assign(this.BackingField).Set(Crumb.GetParameter(0)).Replace(); }