Fix "ref" parameter (#61)
This commit is contained in:
@@ -68,11 +68,13 @@ namespace ProxyInterfaceConsumer
|
||||
public void In_Out_Ref1(in int a, out int b, ref int c)
|
||||
{
|
||||
b = 1;
|
||||
c++;
|
||||
}
|
||||
|
||||
public int In_Out_Ref2(in Address a, out Address b, ref Address c)
|
||||
{
|
||||
b = new Address();
|
||||
c.HouseNumber = 11;
|
||||
return 404;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ internal static class MethodParameterBuilder
|
||||
{
|
||||
stringBuilder.Append(parameterSymbol.GetAttributesPrefix()); // "" or [NotNullWhen(true)]
|
||||
stringBuilder.Append(parameterSymbol.GetParamsPrefix()); // "" or "params "
|
||||
stringBuilder.Append(parameterSymbol.GetRefPrefix()); // "" or "out "
|
||||
stringBuilder.Append(parameterSymbol.GetRefKindPrefix()); // "" or "out "
|
||||
stringBuilder.AppendFormat("{0} ", type); // string or another type
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,12 @@ internal static class ParameterSymbolExtensions
|
||||
{
|
||||
private const string ParameterValueNull = "null";
|
||||
|
||||
public static string GetRefPrefix(this IParameterSymbol ps)
|
||||
public static bool IsRef(this IParameterSymbol ps)
|
||||
{
|
||||
return ps.RefKind is RefKind.Ref or RefKind.RefReadOnly;
|
||||
}
|
||||
|
||||
public static string GetRefKindPrefix(this IParameterSymbol ps)
|
||||
{
|
||||
return ps.RefKind switch
|
||||
{
|
||||
|
||||
@@ -4,6 +4,11 @@ namespace ProxyInterfaceSourceGenerator.Extensions;
|
||||
|
||||
internal static class StringExtensions
|
||||
{
|
||||
public static string IIf(this bool value, string valueTrue, string valueFalse = "")
|
||||
{
|
||||
return value ? valueTrue : valueFalse;
|
||||
}
|
||||
|
||||
// See https://andrewlock.net/why-is-string-gethashcode-different-each-time-i-run-my-program-in-net-core/
|
||||
public static string GetDeterministicHashCodeAsString(this string str)
|
||||
{
|
||||
|
||||
@@ -208,7 +208,9 @@ using System;
|
||||
var type = GetParameterType(parameterSymbol, out _);
|
||||
|
||||
methodParameters.Add(MethodParameterBuilder.Build(parameterSymbol, type));
|
||||
invokeParameters.Add($"{parameterSymbol.GetRefPrefix()}{parameterSymbol.GetSanitizedName()}_");
|
||||
|
||||
// Do not add the '_' for a 'ref' parameter.
|
||||
invokeParameters.Add($"{parameterSymbol.GetRefKindPrefix()}{parameterSymbol.GetSanitizedName()}{(!parameterSymbol.IsRef()).IIf("_")}");
|
||||
}
|
||||
|
||||
string overrideOrVirtual = string.Empty;
|
||||
@@ -235,8 +237,9 @@ using System;
|
||||
}
|
||||
|
||||
str.AppendLine($" public {overrideOrVirtual}{returnTypeAsString} {method.GetMethodNameWithOptionalTypeParameters()}({string.Join(", ", methodParameters)}){whereStatement}");
|
||||
str.AppendLine(" {");
|
||||
foreach (var ps in method.Parameters)
|
||||
str.AppendLine(@" {");
|
||||
|
||||
foreach (var ps in method.Parameters.Where(p => !p.IsRef()))
|
||||
{
|
||||
var type = FixType(ps.Type.ToString());
|
||||
string normalOrMap = $" = {ps.GetSanitizedName()}";
|
||||
|
||||
+1
-3
@@ -76,10 +76,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
|
||||
|
||||
public void In_Out_Ref1(in int a, out int b, ref int c)
|
||||
{
|
||||
int a_ = a;
|
||||
int b_;
|
||||
int c_ = c;
|
||||
_Instance.In_Out_Ref1(in a_, out b_, ref c_);
|
||||
_Instance.In_Out_Ref1(in a, out b_, ref c);
|
||||
b = b_;
|
||||
}
|
||||
|
||||
|
||||
+1
-3
@@ -103,10 +103,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
|
||||
|
||||
public void In_Out_Ref1(in int a, out int b, ref int c)
|
||||
{
|
||||
int a_ = a;
|
||||
int b_;
|
||||
int c_ = c;
|
||||
_Instance.In_Out_Ref1(in a_, out b_, ref c_);
|
||||
_Instance.In_Out_Ref1(in a, out b_, ref c);
|
||||
b = b_;
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
[
|
||||
{
|
||||
HintName: ProxyInterfaceGenerator.ProxyAttribute.g.cs,
|
||||
HintName: ProxyInterfaceGenerator.Extra.g.cs,
|
||||
Source:
|
||||
//----------------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
@@ -16,7 +16,7 @@ using System;
|
||||
namespace ProxyInterfaceGenerator
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Interface)]
|
||||
public class ProxyAttribute : Attribute
|
||||
internal sealed class ProxyAttribute : Attribute
|
||||
{
|
||||
public Type Type { get; }
|
||||
public bool ProxyBaseClasses { get; }
|
||||
|
||||
@@ -273,7 +273,7 @@ public class ProxyInterfaceSourceGeneratorTest
|
||||
public void GenerateFiles_ForSingleClass_Should_GenerateCorrectFiles()
|
||||
{
|
||||
// Arrange
|
||||
var attributeFilename = "ProxyInterfaceGenerator.ProxyAttribute.g.cs";
|
||||
var attributeFilename = "ProxyInterfaceGenerator.Extra.g.cs";
|
||||
var interfaceFilename = "ProxyInterfaceSourceGeneratorTests.Source.IPersonExtends.g.cs";
|
||||
var proxyClassFilename = "ProxyInterfaceSourceGeneratorTests.Source.PersonExtendsProxy.g.cs";
|
||||
|
||||
@@ -364,7 +364,7 @@ public class ProxyInterfaceSourceGeneratorTest
|
||||
public void GenerateFiles_ForTwoClasses_Should_GenerateCorrectFiles()
|
||||
{
|
||||
// Arrange
|
||||
var attributeFilename = "ProxyInterfaceGenerator.ProxyAttribute.g.cs";
|
||||
var attributeFilename = "ProxyInterfaceGenerator.Extra.g.cs";
|
||||
var interfaceHumanFilename = "ProxyInterfaceSourceGeneratorTests.Source.IHuman.g.cs";
|
||||
var proxyClassHumanFilename = "ProxyInterfaceSourceGeneratorTests.Source.HumanProxy.g.cs";
|
||||
var interfacePersonFilename = "ProxyInterfaceSourceGeneratorTests.Source.IPerson.g.cs";
|
||||
@@ -440,5 +440,12 @@ public class ProxyInterfaceSourceGeneratorTest
|
||||
var proxyCode = proxyClassPerson.ToString();
|
||||
if (Write) File.WriteAllText($"../../../Destination/{proxyClassPersonFilename}", proxyCode);
|
||||
proxyCode.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{proxyClassPersonFilename}"));
|
||||
|
||||
var personProxy = new PersonProxy(new Person());
|
||||
|
||||
int c = 100;
|
||||
personProxy.In_Out_Ref1(1, out var b, ref c);
|
||||
|
||||
c.Should().Be(101);
|
||||
}
|
||||
}
|
||||
@@ -83,6 +83,7 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
|
||||
public void In_Out_Ref1(in int a, out int b, ref int c)
|
||||
{
|
||||
b = 1;
|
||||
c++;
|
||||
}
|
||||
|
||||
public double[,] Out_MultiDimensionIssue54(out double[,] x)
|
||||
|
||||
Reference in New Issue
Block a user