Implement Complex

This commit is contained in:
Stef Heyenrath
2021-07-25 11:16:48 +02:00
parent 9326899151
commit e00c0c7706
9 changed files with 51 additions and 16 deletions
@@ -0,0 +1,7 @@
namespace ProxyInterfaceConsumer
{
public class Address
{
public int HouseNumber { get; set; }
}
}
@@ -1,6 +1,6 @@
namespace SourceGeneratorInterface
namespace ProxyInterfaceConsumer
{
[ProxyInterfaceGenerator.Proxy(typeof(SourceGeneratorInterface.Address))]
[ProxyInterfaceGenerator.Proxy(typeof(ProxyInterfaceConsumer.Address))]
public partial interface IAddress
{
}
@@ -1,6 +1,6 @@
namespace SourceGeneratorInterface
namespace ProxyInterfaceConsumer
{
[ProxyInterfaceGenerator.Proxy(typeof(SourceGeneratorInterface.Person))]
[ProxyInterfaceGenerator.Proxy(typeof(ProxyInterfaceConsumer.Person))]
public partial interface IPerson
{
}
@@ -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
{
@@ -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");
@@ -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<string>();
@@ -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();
}
@@ -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();
}
@@ -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());
}