Fix InterfaceName + update Properties, add private Fields

This commit is contained in:
Stef Heyenrath
2021-07-24 09:23:02 +02:00
parent 36fdd645b1
commit afe1710816
12 changed files with 98 additions and 56 deletions
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -21,7 +20,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
_candidateInterfaces = candidateInterfaces;
}
public IEnumerable<Data> GenerateFiles()
public IEnumerable<FileData> GenerateFiles()
{
foreach (var ci in _candidateInterfaces)
{
@@ -31,43 +30,91 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
throw new Exception($"The type '{ci.Value.TypeName}' is not found.");
}
string className = $"{ci.Value.TypeName.Split('.').Last()}";
yield return new Data($"{className}Proxy.cs", CreateProxyClassCode(symbol, className));
yield return new FileData(
$"{ci.Value.ClassName}Proxy.cs",
CreateProxyClassCode(symbol, ci.Value.InterfaceName, ci.Value.ClassName, ci.Value.ProxyAll)
);
}
}
private string CreateProxyClassCode(INamedTypeSymbol symbol, string className) => $@"using System;
private string CreateProxyClassCode(INamedTypeSymbol symbol, string interfaceName, string className, bool proxyAll) => $@"using System;
namespace {symbol.ContainingNamespace}
{{
public class {className}Proxy : I{className}
public class {className}Proxy : {interfaceName}
{{
private {className} _instance;
{GenerateComplexFields(symbol,proxyAll)}
public {className}Proxy({className} instance)
{{
_instance = instance;
}}
{GenerateSimpleProperties(symbol)}
{GenerateProperties(symbol, proxyAll)}
{GenerateMethods(symbol)}
}}
}}";
private string GenerateSimpleProperties(INamedTypeSymbol symbol)
private string GenerateComplexFields(INamedTypeSymbol symbol, bool proxyAll)
{
var str = new StringBuilder();
foreach (var property in GetComplexProperties(symbol, proxyAll))
{
str.AppendLine($" private {property.Type} _{property.Name};");
}
return str.ToString();
}
private string GenerateProperties(INamedTypeSymbol symbol, bool proxyAll)
{
var str = new StringBuilder();
// SimpleProperties
foreach (var property in MemberHelper.GetPublicProperties(symbol, p => p.Type.IsValueType || p.Type.ToString() == "string"))
{
str.AppendLine($" public {property.ToPropertyTextForClass()}");
str.AppendLine();
}
// InterfaceProperties
foreach (var property in MemberHelper.GetPublicProperties(symbol,
p => !(p.Type.IsValueType || p.Type.ToString() == "string"),
p => p.Type.TypeKind == TypeKind.Interface)
)
{
str.AppendLine($" public {property.ToPropertyTextForClass()}");
str.AppendLine();
}
// ComplexProperties
foreach (var property in GetComplexProperties(symbol, proxyAll))
{
str.AppendLine($" public {property.ToPropertyTextForClass()}");
str.AppendLine();
}
return str.ToString();
}
private IEnumerable<IPropertySymbol> GetComplexProperties(INamedTypeSymbol symbol, bool proxyAll)
{
var complexFilters = new List<Func<IPropertySymbol, bool>>
{
p => !(p.Type.IsValueType || p.Type.ToString() == "string"),
p => p.Type.TypeKind != TypeKind.Interface
};
if (proxyAll)
{
}
return MemberHelper.GetPublicProperties(symbol, complexFilters.ToArray());
}
private string GenerateMethods(INamedTypeSymbol symbol)
{
var str = new StringBuilder();