Skip to content

Commit 4cf39bd

Browse files
authored
Support .NET language features: init only setter (#619)
1 parent c27c019 commit 4cf39bd

5 files changed

Lines changed: 47 additions & 1 deletion

File tree

mdoc/Consts.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@ public static class Consts
5050
public const string IsReadOnlyAttribute = "System.Runtime.CompilerServices.IsReadOnlyAttribute";
5151
public const string InAttribute = "System.Runtime.InteropServices.InAttribute";
5252
public const string TupleElementNamesAttribute = "System.Runtime.CompilerServices.TupleElementNamesAttribute";
53+
public const string IsExternalInit = "System.Runtime.CompilerServices.IsExternalInit";
5354
}
5455
}

mdoc/Mono.Documentation/Updater/Formatters/CSharpFullMemberFormatter.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,14 @@ protected override string GetPropertyDeclaration (PropertyDefinition property)
725725
{
726726
if (set_visible != visibility)
727727
buf.Append (' ').Append (set_visible);
728-
buf.Append (" set;");
728+
if (property.SetMethod.ReturnType is RequiredModifierType returnType && returnType.ModifierType.FullName == Consts.IsExternalInit)
729+
{
730+
buf.Append(" init;");
731+
}
732+
else
733+
{
734+
buf.Append(" set;");
735+
}
729736
}
730737
buf.Append (" }");
731738

mdoc/mdoc.Test/FormatterTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,17 @@ public void CSharpTupleNamesMethodTest(string methodName, string expectedSignatu
453453
Assert.AreEqual(expectedSignature, methodSignature);
454454
}
455455

456+
[TestCase("Property1", "public int Property1 { get; set; }")]
457+
[TestCase("Property2", "public int Property2 { get; init; }")]
458+
[TestCase("Property3", "public int Property3 { get; protected init; }")]
459+
[TestCase("Item", "public int this[int index] { get; init; }")]
460+
public void CSharpInitOnlySetterTest(string propertyName, string expectedSignature)
461+
{
462+
var property = GetProperty(typeof(SampleClasses.InitOnlySetter), p => p.Name == propertyName);
463+
var propertySignature = formatter.GetDeclaration(property);
464+
Assert.AreEqual(expectedSignature, propertySignature);
465+
}
466+
456467
#region Helper Methods
457468
string RealTypeName(string name){
458469
switch (name) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace mdoc.Test.SampleClasses
2+
{
3+
public class InitOnlySetter
4+
{
5+
public int Property1 { get; set; }
6+
public int Property2 { get; init; }
7+
public int Property3 { get; protected init; }
8+
public int this[int index]
9+
{
10+
get
11+
{
12+
throw null;
13+
}
14+
init
15+
{
16+
throw null;
17+
}
18+
}
19+
}
20+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System.ComponentModel;
2+
3+
namespace System.Runtime.CompilerServices
4+
{
5+
[EditorBrowsable(EditorBrowsableState.Never)]
6+
internal class IsExternalInit { }
7+
}

0 commit comments

Comments
 (0)