ReadOnlyAutoProperty Mutator

Mutator that takes an auto-property readonly, i.e. can only be assigned in the constructor and has a readonly backing field.
  • Defined in: CciSharp.ReadOnlyAutoProperty.dll

Example

  • original code
class Foo {
    [ReadOnly]
    public int Value {get; private set;}
    public Foo(int value) {
        this.Value = value;
    }
}

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
class ReadOnlyAttribute : Attribute { }
  • rewritten code
class Foo {
    readonly int value; // this field is readonly
    [ReadOnly]
    public int Value {get { return this.value; } } // the setter was removed
    public Foo(int value) {
        this.value = value; // all calls to the setter were replaced by direct access to the field
    }
}

Requirements:

  • an non-virtual instance auto-property with a private setter
  • marked with a ReadOnlyAttribute. It does not matter in which namespace or assembly this attribute lives, the tool only does name matching on the attribute
  • the setter is only called from the constructor

Last edited Dec 30, 2009 at 8:43 AM by pelikhan, version 6

Comments

No comments yet.