Fix "ref" parameter (#61)
This commit is contained in:
@@ -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()}";
|
||||
|
||||
Reference in New Issue
Block a user