PropertyBuilder¶
Create property declarations.
See also: Emit Overview | TypeBuilder | FieldBuilder
Factory Methods¶
| Method | Description |
|---|---|
For(string name, string type) |
Create a property |
Parse(string signature) |
Parse from signature (e.g., "public string Name { get; set; }") |
Accessor Styles¶
// Auto-property { get; set; }
prop.WithAutoPropertyAccessors()
// Read-only auto-property { get; }
prop.WithAutoPropertyAccessors().AsReadOnly()
// Composable auto accessors (individual get/set/init)
prop.WithAutoGetter() // { get; }
prop.WithAutoGetter().WithAutoSetter() // { get; set; }
prop.WithAutoGetter().WithAutoInitSetter() // { get; init; }
prop.WithAutoSetter() // { set; }
prop.WithAutoInitSetter() // { init; }
// Expression-bodied getter => expression
prop.WithGetter("_name")
prop.WithGetter("=> _name") // "=>" is optional
// Block-bodied getter
prop.WithGetter(body => body
.AddStatement("if (_name == null) _name = LoadName();")
.AddReturn("_name"))
// Block-bodied setter
prop.WithSetter(body => body
.AddStatement("_name = value;")
.AddStatement("OnPropertyChanged();"))
Modifiers¶
prop.WithAccessibility(Accessibility.Public)
prop.WithAccessibility("public") // from snapshot or ValidSymbol.AccessibilityString
prop.AsStatic()
prop.AsVirtual()
prop.AsOverride()
prop.AsAbstract()
prop.AsReadOnly() // removes setter
Initialization¶
prop.WithInitializer("new()")
prop.WithInitializer("default")
prop.WithInitializer("\"Default Value\"")
prop.WithBackingField("_name") // references a backing field