diff --git a/src-examples/ProxyInterfaceConsumer/Properties/launchSettings.json b/src-examples/ProxyInterfaceConsumer/Properties/launchSettings.json new file mode 100644 index 0000000..33504c9 --- /dev/null +++ b/src-examples/ProxyInterfaceConsumer/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "WSL": { + "commandName": "WSL2", + "distributionName": "" + } + } +} \ No newline at end of file diff --git a/src/ProxyInterfaceSourceGenerator/Extensions/NamedTypeSymbolExtensions.cs b/src/ProxyInterfaceSourceGenerator/Extensions/NamedTypeSymbolExtensions.cs index c9afff2..ef94c9c 100644 --- a/src/ProxyInterfaceSourceGenerator/Extensions/NamedTypeSymbolExtensions.cs +++ b/src/ProxyInterfaceSourceGenerator/Extensions/NamedTypeSymbolExtensions.cs @@ -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(); } - + /// /// See https://stackoverflow.com/questions/24157101/roslyns-gettypebymetadataname-and-generic-types diff --git a/src/ProxyInterfaceSourceGenerator/Extensions/ParameterSymbolExtensions.cs b/src/ProxyInterfaceSourceGenerator/Extensions/ParameterSymbolExtensions.cs index 1d32edd..bc2682c 100644 --- a/src/ProxyInterfaceSourceGenerator/Extensions/ParameterSymbolExtensions.cs +++ b/src/ProxyInterfaceSourceGenerator/Extensions/ParameterSymbolExtensions.cs @@ -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) { diff --git a/src/ProxyInterfaceSourceGenerator/Extensions/PropertySymbolExtensions.cs b/src/ProxyInterfaceSourceGenerator/Extensions/PropertySymbolExtensions.cs index 094c08f..45ee4ae 100644 --- a/src/ProxyInterfaceSourceGenerator/Extensions/PropertySymbolExtensions.cs +++ b/src/ProxyInterfaceSourceGenerator/Extensions/PropertySymbolExtensions.cs @@ -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}}}"); } } \ No newline at end of file diff --git a/src/ProxyInterfaceSourceGenerator/FileGenerators/BaseGenerator.cs b/src/ProxyInterfaceSourceGenerator/FileGenerators/BaseGenerator.cs index d495ae9..4dc82d8 100644 --- a/src/ProxyInterfaceSourceGenerator/FileGenerators/BaseGenerator.cs +++ b/src/ProxyInterfaceSourceGenerator/FileGenerators/BaseGenerator.cs @@ -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 GetMethodParameters(ImmutableArray parameters, bool includeType) + { + var methodParameters = new List(); + 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; + } } \ No newline at end of file diff --git a/src/ProxyInterfaceSourceGenerator/FileGenerators/PartialInterfacesGenerator.cs b/src/ProxyInterfaceSourceGenerator/FileGenerators/PartialInterfacesGenerator.cs index db03085..3f3fbc8 100644 --- a/src/ProxyInterfaceSourceGenerator/FileGenerators/PartialInterfacesGenerator.cs +++ b/src/ProxyInterfaceSourceGenerator/FileGenerators/PartialInterfacesGenerator.cs @@ -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(); - 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(); + //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); diff --git a/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.cs b/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.cs index bb372fb..478af94 100644 --- a/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.cs +++ b/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.cs @@ -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(); } diff --git a/src/ProxyInterfaceSourceGenerator/Models/Context.cs b/src/ProxyInterfaceSourceGenerator/Models/Context.cs index ae7b059..3aec614 100644 --- a/src/ProxyInterfaceSourceGenerator/Models/Context.cs +++ b/src/ProxyInterfaceSourceGenerator/Models/Context.cs @@ -11,5 +11,5 @@ internal record Context public IDictionary CandidateInterfaces { get; init; } = default!; - public Dictionary ReplacedTypes { get; } = new Dictionary(); + public Dictionary ReplacedTypes { get; } = new(); } \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IPerson.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IPerson.g.cs index 8fd5b4e..b77fa7d 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IPerson.g.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IPerson.g.cs @@ -14,6 +14,10 @@ namespace ProxyInterfaceSourceGeneratorTests.Source { public partial interface IPerson { + ProxyInterfaceSourceGeneratorTests.Source.MyStruct this[int i] { get; set; } + + ProxyInterfaceSourceGeneratorTests.Source.MyStruct this[int i, string s] { get; set; } + string Name { get; set; } string? StringNullable { get; set; } diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.PersonProxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.PersonProxy.g.cs index 80bd704..8cd92c9 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.PersonProxy.g.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.PersonProxy.g.cs @@ -19,6 +19,10 @@ namespace ProxyInterfaceSourceGeneratorTests.Source public ProxyInterfaceSourceGeneratorTests.Source.Human _InstanceBase { get; } + public ProxyInterfaceSourceGeneratorTests.Source.MyStruct this[int i] { get => _Instance[i]; set => _Instance[i] = value; } + + public ProxyInterfaceSourceGeneratorTests.Source.MyStruct this[int i, string s] { get => _Instance[i, s]; set => _Instance[i, s] = value; } + public string Name { get => _Instance.Name; set => _Instance.Name = value; } public string? StringNullable { get => _Instance.StringNullable; set => _Instance.StringNullable = value; } diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Source/Person.cs b/tests/ProxyInterfaceSourceGeneratorTests/Source/Person.cs index 9d585cb..8857528 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Source/Person.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Source/Person.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Net.Http; using System.Threading; using System.Threading.Tasks; @@ -7,6 +6,20 @@ namespace ProxyInterfaceSourceGeneratorTests.Source { public class Person : Human { + private readonly MyStruct[] _arr = new MyStruct[1]; + + public MyStruct this[int i] + { + get { return _arr[i]; } + set { _arr[i] = value; } + } + + public MyStruct this[int i, string s] + { + get { return _arr[i]; } + set { _arr[i] = value; } + } + public IList AddHuman(Human h) { return new List { h, new Human { IsAlive = true } }; @@ -28,7 +41,7 @@ namespace ProxyInterfaceSourceGeneratorTests.Source { return $"Hello {name} !"; } - + public string HelloWorld2(string? name = "x") { return $"Hello {name} !";