Skip to content

Commit 8b71d37

Browse files
committed
- Update SampleLibrary/SampleLibrary project to .Net 10.
- Add support for parameter modifieres e.g. in, out.
1 parent 5a7b393 commit 8b71d37

5 files changed

Lines changed: 66 additions & 5 deletions

File tree

DecoratorGenerator.UnitTests/Tests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,29 @@ public async Task OneInterface_NestedNamespace() {
134134
}.RunAsync();
135135
}
136136

137+
[Test]
138+
public async Task OneInterface_OutParameter() {
139+
var source = await ReadCSharpFile<IOutParameter>(true);
140+
var generated = await ReadCSharpFile<OutParameterDecorator>(true);
141+
142+
await new VerifyCS.Test
143+
{
144+
TestState = {
145+
ReferenceAssemblies = ReferenceAssemblies.Net.Net90,
146+
AdditionalReferences =
147+
{
148+
implementationAssembly,
149+
GetAssembly("TestLibrary")
150+
},
151+
Sources = { source },
152+
GeneratedSources =
153+
{
154+
(typeof(Main), "OutParameterDecorator.generated.cs", SourceText.From(generated, Encoding.UTF8, SourceHashAlgorithm.Sha256)),
155+
},
156+
},
157+
}.RunAsync();
158+
}
159+
137160
[Test]
138161
public async Task TwoInterfaces() {
139162
var sourceOne = await ReadCSharpFile<IBird>(true);

DecoratorGenerator/OutputGenerator.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,24 @@ private static string FormatInterfaceConstraintTypes(INamedTypeSymbol @interface
5858

5959
var displayMethods = methods.Select(method => {
6060
var typeParametersStrings = method.TypeParameters.Select(t => t.ToDisplayString());
61-
var parametersStrings = method.Parameters.Select(p => $@"{p.Type} {p.Name}");
61+
var parametersStrings = method.Parameters.Select(p => {
62+
var modifier = p.RefKind == RefKind.None
63+
? string.Empty
64+
: $"{p.RefKind.ToString().ToLower()} ";
65+
66+
return $@"{modifier}{p.Type} {p.Name}";
67+
});
6268
var formattedAccessibility = (method.ReturnType.DeclaredAccessibility != Accessibility.NotApplicable ? method.ReturnType.DeclaredAccessibility : Accessibility.Public).ToString().ToLower();
6369
var formattedGenericTypeParameters = method.IsGenericMethod ? $@"<{string.Join(", ", typeParametersStrings)}>" : string.Empty;
6470
var formattedConstraints = CreateFormattedConstraints(method.TypeParameters);
6571
var signature = $@"{formattedAccessibility} virtual {method.ReturnType} {method.Name}{formattedGenericTypeParameters}({string.Join(", ", parametersStrings)}){(formattedConstraints != string.Empty ? $@" {formattedConstraints}" : string.Empty)}";
66-
var callParameters = $@"{string.Join(", ", method.Parameters.Select(p => p.Name))}";
72+
var callParameters = $@"{string.Join(", ", method.Parameters.Select(p => {
73+
var modifier = p.RefKind == RefKind.None
74+
? string.Empty
75+
: $"{p.RefKind.ToString().ToLower()} ";
76+
77+
return $"{modifier}{p.Name}";
78+
}))}";
6779

6880
var call = $@"{targetFieldName}.{method.Name}{(method.IsGenericMethod ? $@"<{string.Join(", ", typeParametersStrings)}>" : string.Empty)}({callParameters})";
6981

@@ -123,8 +135,7 @@ private static string CreateFormattedConstraints(ImmutableArray<ITypeParameterSy
123135

124136
private static IEnumerable<string> FormatDisplayMethods(IEnumerable<(string signature, string call, ITypeSymbol returnType)> displayMethods) {
125137
return displayMethods.Select(method => {
126-
return
127-
$@" {method.signature} {{
138+
return $@" {method.signature} {{
128139
{(method.returnType.Name == "Void" ? string.Empty : "return ")}{method.call};
129140
}}";
130141
});

SampleLibrary/SampleLibrary.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
</PropertyGroup>

TestLibrary/IOutParameter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using DecoratorGenerator;
2+
3+
namespace SampleLibrary;
4+
5+
[Decorate]
6+
public interface IOutParameter
7+
{
8+
bool VerifySomething(string input, out string output);
9+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// <auto-generated/>
2+
#nullable restore
3+
namespace SampleLibrary;
4+
5+
public abstract class OutParameterDecorator : IOutParameter
6+
{
7+
private IOutParameter outParameter;
8+
9+
protected OutParameterDecorator(IOutParameter outParameter) {
10+
this.outParameter = outParameter;
11+
}
12+
13+
14+
15+
public virtual bool VerifySomething(string input, out string output) {
16+
return outParameter.VerifySomething(input, out output);
17+
}
18+
}

0 commit comments

Comments
 (0)