diff --git a/src-examples/ProxyInterfaceConsumer/Address.cs b/src-examples/ProxyInterfaceConsumer/Address.cs new file mode 100644 index 0000000..c4db154 --- /dev/null +++ b/src-examples/ProxyInterfaceConsumer/Address.cs @@ -0,0 +1,7 @@ +namespace ProxyInterfaceConsumer +{ + public class Address + { + public int HouseNumber { get; set; } + } +} \ No newline at end of file diff --git a/src-examples/ProxyInterfaceConsumer/IAddress.cs b/src-examples/ProxyInterfaceConsumer/IAddress.cs index ee6b033..514c7fd 100644 --- a/src-examples/ProxyInterfaceConsumer/IAddress.cs +++ b/src-examples/ProxyInterfaceConsumer/IAddress.cs @@ -1,6 +1,6 @@ -namespace SourceGeneratorInterface +namespace ProxyInterfaceConsumer { - [ProxyInterfaceGenerator.Proxy(typeof(SourceGeneratorInterface.Address))] + [ProxyInterfaceGenerator.Proxy(typeof(ProxyInterfaceConsumer.Address))] public partial interface IAddress { } diff --git a/src-examples/ProxyInterfaceConsumer/IPerson.cs b/src-examples/ProxyInterfaceConsumer/IPerson.cs index 08336e2..e565cb5 100644 --- a/src-examples/ProxyInterfaceConsumer/IPerson.cs +++ b/src-examples/ProxyInterfaceConsumer/IPerson.cs @@ -1,6 +1,6 @@ -namespace SourceGeneratorInterface +namespace ProxyInterfaceConsumer { - [ProxyInterfaceGenerator.Proxy(typeof(SourceGeneratorInterface.Person))] + [ProxyInterfaceGenerator.Proxy(typeof(ProxyInterfaceConsumer.Person))] public partial interface IPerson { } diff --git a/src-examples/ProxyInterfaceConsumer/Person.cs b/src-examples/ProxyInterfaceConsumer/Person.cs index b4524cc..6ba0be7 100644 --- a/src-examples/ProxyInterfaceConsumer/Person.cs +++ b/src-examples/ProxyInterfaceConsumer/Person.cs @@ -1,6 +1,6 @@ using Microsoft.CodeAnalysis; -namespace SourceGeneratorInterface +namespace ProxyInterfaceConsumer { public class Person { @@ -31,10 +31,7 @@ namespace SourceGeneratorInterface public INamedTypeSymbol MyNamedTypeSymbol { get;set; } } - public class Address - { - public int X { get; } - } + public enum E { diff --git a/src-examples/ProxyInterfaceConsumer/Program.cs b/src-examples/ProxyInterfaceConsumer/Program.cs index 46f2716..78effc7 100644 --- a/src-examples/ProxyInterfaceConsumer/Program.cs +++ b/src-examples/ProxyInterfaceConsumer/Program.cs @@ -1,6 +1,6 @@ using System; -namespace SourceGeneratorInterface +namespace ProxyInterfaceConsumer { public class Program { @@ -8,6 +8,8 @@ namespace SourceGeneratorInterface { IPerson p = new PersonProxy(new Person()); p.Name = "test"; + p.Address = new AddressProxy(new Address { HouseNumber = 42 }); + //p.MyNamedTypeSymbol = null; //p.Compilation = null; //p.Add("x"); diff --git a/src/ProxyInterfaceSourceGenerator/Extensions/SymbolExtensions.cs b/src/ProxyInterfaceSourceGenerator/Extensions/SymbolExtensions.cs index 3509a83..cea342e 100644 --- a/src/ProxyInterfaceSourceGenerator/Extensions/SymbolExtensions.cs +++ b/src/ProxyInterfaceSourceGenerator/Extensions/SymbolExtensions.cs @@ -39,6 +39,15 @@ namespace ProxyInterfaceSourceGenerator.Extensions return $"{property.Type} {property.Name} {{ {get}{set}}}"; } + public static string ToPropertyTextForClass(this IPropertySymbol property, string interfaceName, string className) + { + var classNameProxy = $"{className}Proxy"; + var get = property.GetMethod != null ? $"get => new {classNameProxy}(_Instance.{property.Name}); " : string.Empty; + var set = property.SetMethod != null ? $"set => _Instance.{property.Name} = (({classNameProxy}) value)._Instance; " : string.Empty; + + return $"{interfaceName} {property.Name} {{ {get}{set}}}"; + } + public static string ToMethodText(this IMethodSymbol method) { var parameters = new List(); diff --git a/src/ProxyInterfaceSourceGenerator/FileGenerators/PartialInterfacesGenerator.cs b/src/ProxyInterfaceSourceGenerator/FileGenerators/PartialInterfacesGenerator.cs index 203f3d5..2e83792 100644 --- a/src/ProxyInterfaceSourceGenerator/FileGenerators/PartialInterfacesGenerator.cs +++ b/src/ProxyInterfaceSourceGenerator/FileGenerators/PartialInterfacesGenerator.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using System.Text; using Microsoft.CodeAnalysis; using ProxyInterfaceSourceGenerator.Enums; @@ -92,8 +93,17 @@ namespace {symbol.ContainingNamespace} // } //} //else + + var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == property.Type.ToString()); + if (existing is not null) + { + str.AppendLine($" {property.ToPropertyText(existing.InterfaceName)}"); + } + else + { + str.AppendLine($" {property.ToPropertyText()}"); + } - str.AppendLine($" {property.ToPropertyText()}"); str.AppendLine(); } diff --git a/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.cs b/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.cs index 67cc09a..a40a9d3 100644 --- a/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.cs +++ b/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using System.Text; using Microsoft.CodeAnalysis; using ProxyInterfaceSourceGenerator.Enums; @@ -81,7 +82,16 @@ namespace {symbol.ContainingNamespace} // ComplexProperties foreach (var property in MemberHelper.GetPublicProperties(symbol, p => p.GetTypeEnum() == TypeEnum.Complex)) { - str.AppendLine($" public {property.ToPropertyTextForClass()}"); + var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == property.Type.ToString()); + if (existing is not null) + { + str.AppendLine($" public {property.ToPropertyTextForClass(existing.InterfaceName, existing.ClassName)}"); + } + else + { + str.AppendLine($" public {property.ToPropertyTextForClass()}"); + } + str.AppendLine(); } diff --git a/src/ProxyInterfaceSourceGenerator/ProxyInterfaceCodeGenerator.cs b/src/ProxyInterfaceSourceGenerator/ProxyInterfaceCodeGenerator.cs index 17dffc6..088c410 100644 --- a/src/ProxyInterfaceSourceGenerator/ProxyInterfaceCodeGenerator.cs +++ b/src/ProxyInterfaceSourceGenerator/ProxyInterfaceCodeGenerator.cs @@ -13,10 +13,10 @@ namespace ProxyInterfaceSourceGenerator public void Initialize(GeneratorInitializationContext context) { - if (!System.Diagnostics.Debugger.IsAttached) - { - System.Diagnostics.Debugger.Launch(); - } + //if (!System.Diagnostics.Debugger.IsAttached) + //{ + // System.Diagnostics.Debugger.Launch(); + //} context.RegisterForSyntaxNotifications(() => new ProxySyntaxReceiver()); }