@@ -1,4 +1,3 @@
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ProxyInterfaceSourceGenerator.Extensions;
|
||||
@@ -45,7 +44,7 @@ internal static class NamedTypeSymbolExtensions
|
||||
return namedTypeSymbol.OriginalDefinition.ToString();// str.ToString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// See https://stackoverflow.com/questions/24157101/roslyns-gettypebymetadataname-and-generic-types
|
||||
|
||||
@@ -19,8 +19,7 @@ internal static class ParameterSymbolExtensions
|
||||
};
|
||||
}
|
||||
|
||||
public static string GetParamsPrefix(this IParameterSymbol ps) =>
|
||||
ps.IsParams ? "params " : string.Empty;
|
||||
public static string GetParamsPrefix(this IParameterSymbol ps) => ps.IsParams ? "params " : string.Empty;
|
||||
|
||||
public static string GetDefaultValue(this IParameterSymbol ps)
|
||||
{
|
||||
|
||||
@@ -1,55 +1,19 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using ProxyInterfaceSourceGenerator.Enums;
|
||||
using ProxyInterfaceSourceGenerator.Models;
|
||||
|
||||
namespace ProxyInterfaceSourceGenerator.Extensions;
|
||||
|
||||
internal static class PropertySymbolExtensions
|
||||
{
|
||||
public static TypeEnum GetTypeEnum(this IPropertySymbol p) =>
|
||||
p.Type.GetTypeEnum();
|
||||
public static TypeEnum GetTypeEnum(this IPropertySymbol p) => p.Type.GetTypeEnum();
|
||||
|
||||
public static string ToPropertyText(this IPropertySymbol property, string? overrideType = null)
|
||||
public static (string PropertyType, string? PropertyName, string GetSet) ToPropertyDetails(this IPropertySymbol property, string? overrideType = null)
|
||||
{
|
||||
var get = property.GetMethod != null ? "get; " : string.Empty;
|
||||
var set = property.SetMethod != null ? "set; " : string.Empty;
|
||||
|
||||
var type = !string.IsNullOrEmpty(overrideType) ? overrideType : $"{property.Type}";
|
||||
|
||||
return $"{type} {property.GetSanitizedName()} {{ {get}{set}}}";
|
||||
}
|
||||
|
||||
public static string ToPropertyTextForClass(this IPropertySymbol property, ClassSymbol targetClassSymbol)
|
||||
{
|
||||
string instance = !property.IsStatic ?
|
||||
"_Instance" :
|
||||
$"{targetClassSymbol.Symbol}";
|
||||
|
||||
var get = property.GetMethod != null ? $"get => {instance}.{property.GetSanitizedName()}; " : string.Empty;
|
||||
var set = property.SetMethod != null ? $"set => {instance}.{property.GetSanitizedName()} = value; " : string.Empty;
|
||||
|
||||
return $"{property.Type} {property.GetSanitizedName()} {{ {get}{set}}}";
|
||||
}
|
||||
|
||||
public static string ToPropertyTextForClass(this IPropertySymbol property, ClassSymbol targetClassSymbol, string overrideType)
|
||||
{
|
||||
string instance = !property.IsStatic ?
|
||||
"_Instance" :
|
||||
$"{targetClassSymbol.Symbol}";
|
||||
|
||||
string overrideOrVirtual = string.Empty;
|
||||
if (property.IsOverride)
|
||||
{
|
||||
overrideOrVirtual = "override ";
|
||||
}
|
||||
else if (property.IsVirtual)
|
||||
{
|
||||
overrideOrVirtual = "virtual ";
|
||||
}
|
||||
|
||||
var get = property.GetMethod != null ? $"get => _mapper.Map<{overrideType}>({instance}.{property.GetSanitizedName()}); " : string.Empty;
|
||||
var set = property.SetMethod != null ? $"set => {instance}.{property.GetSanitizedName()} = _mapper.Map<{property.Type}>(value); " : string.Empty;
|
||||
|
||||
return $"{overrideOrVirtual}{overrideType} {property.GetSanitizedName()} {{ {get}{set}}}";
|
||||
return (type!, property.GetSanitizedName(), $"{{ {get}{set}}}");
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using ProxyInterfaceSourceGenerator.Enums;
|
||||
using ProxyInterfaceSourceGenerator.Extensions;
|
||||
using ProxyInterfaceSourceGenerator.Models;
|
||||
|
||||
@@ -200,4 +202,21 @@ internal abstract class BaseGenerator
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected IList<string> GetMethodParameters(ImmutableArray<IParameterSymbol> parameters, bool includeType)
|
||||
{
|
||||
var methodParameters = new List<string>();
|
||||
foreach (var ps in parameters)
|
||||
{
|
||||
string t = string.Empty;
|
||||
if (includeType)
|
||||
{
|
||||
var type = ps.GetTypeEnum() == TypeEnum.Complex ? GetParameterType(ps, out _) : ps.Type.ToString();
|
||||
t = $"{ps.GetParamsPrefix()}{ps.GetRefPrefix()}{type} ";
|
||||
}
|
||||
methodParameters.Add($"{t}{ps.GetSanitizedName()}{ps.GetDefaultValue()}");
|
||||
}
|
||||
|
||||
return methodParameters;
|
||||
}
|
||||
}
|
||||
@@ -87,14 +87,18 @@ namespace {ns}
|
||||
foreach (var property in MemberHelper.GetPublicProperties(targetClassSymbol, proxyBaseClasses))
|
||||
{
|
||||
var type = GetPropertyType(property, out var isReplaced);
|
||||
if (isReplaced)
|
||||
|
||||
(string propertyType, string? propertyName, string getSet) = isReplaced ?
|
||||
property.ToPropertyDetails(type) :
|
||||
property.ToPropertyDetails();
|
||||
|
||||
if (property.IsIndexer)
|
||||
{
|
||||
str.AppendLine($" {property.ToPropertyText(type)}");
|
||||
}
|
||||
else
|
||||
{
|
||||
str.AppendLine($" {property.ToPropertyText()}");
|
||||
var methodParameters = GetMethodParameters(property.Parameters, true);
|
||||
propertyName = $"this[{string.Join(", ", methodParameters)}]";
|
||||
}
|
||||
|
||||
str.AppendLine($" {propertyType} {propertyName} {getSet}");
|
||||
str.AppendLine();
|
||||
}
|
||||
|
||||
@@ -106,12 +110,13 @@ namespace {ns}
|
||||
var str = new StringBuilder();
|
||||
foreach (var method in MemberHelper.GetPublicMethods(targetClassSymbol, proxyBaseClasses))
|
||||
{
|
||||
var methodParameters = new List<string>();
|
||||
foreach (var ps in method.Parameters)
|
||||
{
|
||||
var type = ps.GetTypeEnum() == TypeEnum.Complex ? GetParameterType(ps, out _) : ps.Type.ToString();
|
||||
methodParameters.Add($"{ps.GetParamsPrefix()}{ps.GetRefPrefix()}{type} {ps.GetSanitizedName()}{ps.GetDefaultValue()}");
|
||||
}
|
||||
var methodParameters = GetMethodParameters(method.Parameters, true);
|
||||
//var methodParameters = new List<string>();
|
||||
//foreach (var ps in method.Parameters)
|
||||
//{
|
||||
// var type = ps.GetTypeEnum() == TypeEnum.Complex ? GetParameterType(ps, out _) : ps.Type.ToString();
|
||||
// methodParameters.Add($"{ps.GetParamsPrefix()}{ps.GetRefPrefix()}{type} {ps.GetSanitizedName()}{ps.GetDefaultValue()}");
|
||||
//}
|
||||
|
||||
var whereStatement = GetWhereStatementFromMethod(method);
|
||||
|
||||
|
||||
@@ -130,14 +130,45 @@ namespace {pd.Namespace}
|
||||
{
|
||||
var type = GetPropertyType(property, out var isReplaced);
|
||||
|
||||
var instance = !property.IsStatic ?
|
||||
"_Instance" :
|
||||
$"{targetClassSymbol.Symbol}";
|
||||
|
||||
var propertyName = property.GetSanitizedName();
|
||||
var instancePropertyName = $"{instance}.{propertyName}";
|
||||
if (property.IsIndexer)
|
||||
{
|
||||
var parameters = GetMethodParameters(property.Parameters, true);
|
||||
propertyName = $"this[{string.Join(", ", parameters)}]";
|
||||
|
||||
var instanceParameters = GetMethodParameters(property.Parameters, false);
|
||||
instancePropertyName = $"{instance}[{string.Join(", ", instanceParameters)}]";
|
||||
}
|
||||
|
||||
var overrideOrVirtual = string.Empty;
|
||||
if (property.IsOverride)
|
||||
{
|
||||
overrideOrVirtual = "override ";
|
||||
}
|
||||
else if (property.IsVirtual)
|
||||
{
|
||||
overrideOrVirtual = "virtual ";
|
||||
}
|
||||
|
||||
string get;
|
||||
string set;
|
||||
if (isReplaced)
|
||||
{
|
||||
str.AppendLine($" public {property.ToPropertyTextForClass(targetClassSymbol, type)}");
|
||||
get = property.GetMethod != null ? $"get => _mapper.Map<{type}>({instancePropertyName}); " : string.Empty;
|
||||
set = property.SetMethod != null ? $"set => {instancePropertyName} = _mapper.Map<{property.Type}>(value); " : string.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
str.AppendLine($" public {property.ToPropertyTextForClass(targetClassSymbol)}");
|
||||
get = property.GetMethod != null ? $"get => {instancePropertyName}; " : string.Empty;
|
||||
set = property.SetMethod != null ? $"set => {instancePropertyName} = value; " : string.Empty;
|
||||
}
|
||||
|
||||
str.AppendLine($" public {overrideOrVirtual}{type} {propertyName} {{ {get}{set}}}");
|
||||
str.AppendLine();
|
||||
}
|
||||
|
||||
|
||||
@@ -11,5 +11,5 @@ internal record Context
|
||||
|
||||
public IDictionary<InterfaceDeclarationSyntax, ProxyData> CandidateInterfaces { get; init; } = default!;
|
||||
|
||||
public Dictionary<string, string> ReplacedTypes { get; } = new Dictionary<string, string>();
|
||||
public Dictionary<string, string> ReplacedTypes { get; } = new();
|
||||
}
|
||||
Reference in New Issue
Block a user