Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public CSharpGeneratorSettings()
GenerateJsonMethods = false;
EnforceFlagEnums = false;
UseRequiredKeyword = false;
WriteAccessor = "set";

ValueGenerator = new CSharpValueGenerator(this);
PropertyNameGenerator = new CSharpPropertyNameGenerator();
Expand Down Expand Up @@ -152,6 +153,9 @@ public CSharpGeneratorSettings()

/// <summary>Gets or sets a value indicating whether the C# 11 "required" keyword should be used for required properties (default: false). </summary>
public bool UseRequiredKeyword { get; set; }

/// <summary> Gets the read accessor of properties ('set' | 'init').</summary>
public string WriteAccessor { get; set; }

/// <summary>Gets or sets a value indicating whether named/referenced dictionaries should be inlined or generated as class with dictionary inheritance.</summary>
public bool InlineNamedDictionaries { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings,

/// <summary>Gets a value indicating whether to use the C# 11 "required" keyword.</summary>
public bool UseRequiredKeyword => _settings.UseRequiredKeyword;

/// <summary> Gets the read accessor of properties ('set' | 'init').</summary>
public string WriteAccessor => _settings.WriteAccessor;

/// <summary>Gets the access modifier of property setters (default: '').</summary>
public string PropertySetterAccessModifier => !string.IsNullOrEmpty(_settings.PropertySetterAccessModifier) ? _settings.PropertySetterAccessModifier + " " : "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
[System.Obsolete{% if property.HasDeprecatedMessage %}({{ property.DeprecatedMessage | literal }}){% endif %}]
{%- endif -%}
{%- template Class.Property.Annotations -%}
public {% if UseRequiredKeyword and property.IsRequired %}required {% endif %}{{ property.Type }} {{ property.PropertyName }}{% if RenderInpc == false and RenderPrism == false %} { get; {% if property.HasSetter and RenderRecord == false %}set; {% elsif RenderRecord and GenerateNativeRecords %}init; {% endif %}}{% if property.HasDefaultValue and RenderRecord == false %} = {{ property.DefaultValue }};{% elsif GenerateNullableReferenceTypes and RenderRecord == false %} = default!;{% endif %}
public {% if UseRequiredKeyword and property.IsRequired %}required {% endif %}{{ property.Type }} {{ property.PropertyName }}{% if RenderInpc == false and RenderPrism == false %} { get; {% if property.HasSetter and RenderRecord == false %}{{ WriteAccessor }}; {% elsif RenderRecord and GenerateNativeRecords %}init; {% endif %}}{% if property.HasDefaultValue and RenderRecord == false and UseRequiredKeyword == false %} = {{ property.DefaultValue }};{% elsif GenerateNullableReferenceTypes and RenderRecord == false and UseRequiredKeyword == false %} = default!;{% endif %}
{%- else -%}
{
get { return {{ property.FieldName }}; }
Expand Down
Loading