Add support for generics (#20)

* wip

* .

* method

* TT

* TT

* .

* .

* mp
This commit is contained in:
Stef Heyenrath
2021-08-01 10:34:25 +02:00
committed by GitHub
parent 296988b13f
commit 390093d007
18 changed files with 322 additions and 169 deletions
@@ -1,6 +1,6 @@
namespace ProxyInterfaceConsumer
{
[ProxyInterfaceGenerator.Proxy(typeof(ProxyInterfaceConsumer.Address))]
// [ProxyInterfaceGenerator.Proxy(typeof(ProxyInterfaceConsumer.Address))]
public partial interface IAddress
{
}
@@ -0,0 +1,7 @@
namespace ProxyInterfaceConsumer
{
[ProxyInterfaceGenerator.Proxy(typeof(ProxyInterfaceConsumer.PersonT<>))]
public partial interface IPersonT<T> where T : struct
{
}
}
@@ -0,0 +1,16 @@
namespace ProxyInterfaceConsumer
{
[ProxyInterfaceGenerator.Proxy(typeof(ProxyInterfaceConsumer.PersonT<,>))]
public partial interface IPersonT<T1, T2>
where T1 : struct
where T2 : class, new()
{
}
[ProxyInterfaceGenerator.Proxy(typeof(ProxyInterfaceConsumer.PersonTT<,>))]
public partial interface IPersonTT<T1, T2>
where T1 : struct
where T2 : class, new()
{
}
}
@@ -27,6 +27,13 @@ namespace ProxyInterfaceConsumer
public IMyInterface MyInterface { get; set; }
public bool TMethod<T1, T2>(int x, T1 t1, T2 t2)
where T1 : struct
where T2 : class, new()
{
return true;
}
public int DefaultValue(int x = 100)
{
return x + 1;
@@ -0,0 +1,12 @@
namespace ProxyInterfaceConsumer
{
public class PersonT<T> where T: struct
{
public T TVal { get; set; }
public T Call(int x, T t)
{
return default;
}
}
}
@@ -0,0 +1,28 @@
namespace ProxyInterfaceConsumer
{
public class PersonT<T1, T2>
where T1 : struct
where T2 : class, new()
{
public T1 TVal1 { get; set; }
public T2 TVal2 { get; set; }
public void Call(int x, T1 t1, T2 t2)
{
}
}
public class PersonTT<T1, T2>
where T1 : struct
where T2 : class, new()
{
public T1 TVal1 { get; set; }
public T2 TVal2 { get; set; }
public void Call(int x, T1 t1, T2 t2)
{
}
}
}
+13 -27
View File
@@ -14,45 +14,31 @@ namespace ProxyInterfaceConsumer
public static void Main()
{
var c = new Clazz
{
Name = "n"
};
var cp = new ClazzProxy(c);
var t = new Test();
t.Cs = new List<Clazz> { c };
var tp = new TestProxy(t);
tp.Cs = new List<IClazz> { cp };
Console.WriteLine(JsonSerializer.Serialize(t, JsonSerializerOptions));
IPersonT<int> pT = new PersonTProxy<int>(new PersonT<int>());
pT.TVal = 1;
Console.WriteLine(JsonSerializer.Serialize(pT, JsonSerializerOptions));
Console.WriteLine(new string('-', 80));
IPersonTT<int, Program> pTT = new PersonTTProxy<int, Program>(new PersonTT<int, Program>());
pTT.TVal1 = 42;
pTT.TVal2 = new Program();
Console.WriteLine(JsonSerializer.Serialize(pTT, JsonSerializerOptions));
Console.WriteLine(new string('-', 80));
IPerson p = new PersonProxy(new Person());
p.Name = "test";
Console.WriteLine("DefaultValue " + p.DefaultValue());
Console.WriteLine("DefaultValue " + p.DefaultValue(42));
var ap = new AddressProxy(new Address { HouseNumber = 42 });
p.Address = ap;
var add = p.AddAddress(ap);
Console.WriteLine("add = " + JsonSerializer.Serialize(add, JsonSerializerOptions));
p.AddAddress(new AddressProxy(new Address { HouseNumber = 1000 }));
//var ap = new AddressProxy(new Address { HouseNumber = 42 });
//p.Address = ap;
//var add = p.AddAddress(ap);
//Console.WriteLine("add = " + JsonSerializer.Serialize(add, JsonSerializerOptions));
//p.AddAddress(new AddressProxy(new Address { HouseNumber = 1000 }));
//p.MyNamedTypeSymbol = null;
//p.Compilation = null;
//p.Add("x");
//p.Void();
Console.WriteLine(JsonSerializer.Serialize(p, JsonSerializerOptions));
//GeneratorExecutionContext g = new GeneratorExecutionContext();
//IGeneratorExecutionContext gc = new GeneratorExecutionContextProxy(g);
//int y = 9;
}
}
public struct Test
{
public int Id { get; set; }
@@ -73,7 +73,7 @@ namespace ProxyInterfaceConsumer
ProxyInterfaceConsumer.Address b_;
ProxyInterfaceConsumer.Address c_ = _mapper.Map<ProxyInterfaceConsumer.Address>(c);
var result_30316242 = _Instance.In_Out_Ref2(in a_, out b_, ref c_);
b = _mapper.Map<IAddress>(_b);
b = _mapper.Map<IAddress>(b_);
return result_30316242;
}
+1 -1
View File
@@ -9,7 +9,7 @@ namespace ProxyInterfaceSourceGenerator
{
public GeneratorExecutionContext GeneratorExecutionContext { get; init; }
public List<ContextData> GeneratedData { get; } = new List<ContextData>();
// public List<ContextData> GeneratedData { get; } = new List<ContextData>();
public IDictionary<InterfaceDeclarationSyntax, ProxyData> CandidateInterfaces { get; init; }
@@ -0,0 +1,14 @@
using System.Linq;
using Microsoft.CodeAnalysis;
namespace ProxyInterfaceSourceGenerator.Extensions
{
internal static class MethodSymbolExtensions
{
public static string GetMethodNameWithOptionalTypeParameters(this IMethodSymbol method) =>
!method.IsGenericMethod ? method.Name : $"{method.Name}<{string.Join(", ", method.TypeParameters.Select(tp => tp.Name))}>";
public static string GetWhereStatement(this IMethodSymbol method) =>
!method.IsGenericMethod ? string.Empty : string.Join("", method.TypeParameters.Select(tp => tp.GetWhereStatement()));
}
}
@@ -0,0 +1,52 @@
using System;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
namespace ProxyInterfaceSourceGenerator.Extensions
{
internal static class NamedTypeSymbolExtensions
{
/// <summary>
/// See https://www.codeproject.com/Articles/861548/Roslyn-Code-Analysis-in-Easy-Samples-Part
/// </summary>
public static string GetFullTypeString(this INamedTypeSymbol namedTypeSymbol)
{
var str = new StringBuilder(namedTypeSymbol.Name);
if (namedTypeSymbol.TypeArguments.Count() > 0)
{
str.AppendFormat("<{0}>", string.Join(", ", namedTypeSymbol.TypeArguments.OfType<INamedTypeSymbol>().Select(typeArg => typeArg.GetFullTypeString())));
}
return str.ToString();
}
public static string ResolveInterfaceNameWithOptionalTypeConstraints(this INamedTypeSymbol namedTypeSymbol, string interfaceName)
{
if (!namedTypeSymbol.IsGenericType)
{
return interfaceName;
}
var str = new StringBuilder($"{interfaceName}<{string.Join(", ", namedTypeSymbol.TypeArguments.Select(ta => ta.Name))}>");
foreach (var typeParameterSymbol in namedTypeSymbol.TypeArguments.OfType<ITypeParameterSymbol>())
{
str.Append(typeParameterSymbol.GetWhereStatement());
}
return str.ToString();
}
/// <summary>
/// See https://stackoverflow.com/questions/24157101/roslyns-gettypebymetadataname-and-generic-types
/// </summary>
public static string ResolveProxyClassName(this INamedTypeSymbol namedTypeSymbol)
{
return !namedTypeSymbol.IsGenericType ?
$"{namedTypeSymbol.Name}Proxy" :
$"{namedTypeSymbol.Name}Proxy<{string.Join(", ", namedTypeSymbol.TypeArguments.Select(ta => ta.Name))}>";
}
}
}
@@ -5,7 +5,7 @@ namespace ProxyInterfaceSourceGenerator.Extensions
internal static class SyntaxNodeUtils
{
// https://stackoverflow.com/questions/20458457/getting-class-fullname-including-namespace-from-roslyn-classdeclarationsyntax
public static bool TryGetParentSyntax<T>(this SyntaxNode syntaxNode, out T result) where T : SyntaxNode
public static bool TryGetParentSyntax<T>(this SyntaxNode? syntaxNode, out T? result) where T : SyntaxNode
{
result = null;
@@ -0,0 +1,40 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
namespace ProxyInterfaceSourceGenerator.Extensions
{
internal static class TypeParameterSymbolExtensions
{
/// <summary>
/// https://www.codeproject.com/Articles/871704/Roslyn-Code-Analysis-in-Easy-Samples-Part-2
/// </summary>
public static string GetWhereStatement(this ITypeParameterSymbol typeParameterSymbol)
{
var constraints = new List<string>();
if (typeParameterSymbol.HasReferenceTypeConstraint)
{
constraints.Add("class");
}
if (typeParameterSymbol.HasValueTypeConstraint)
{
constraints.Add("struct");
}
if (typeParameterSymbol.HasConstructorConstraint)
{
constraints.Add("new()");
}
constraints.AddRange(typeParameterSymbol.ConstraintTypes.OfType<INamedTypeSymbol>().Select(contstraintType => contstraintType.GetFullTypeString()));
if (!constraints.Any())
{
return string.Empty;
}
return $" where {typeParameterSymbol.Name} : {string.Join(", ", constraints)}";
}
}
}
@@ -1,6 +1,5 @@
using System;
using System.Linq;
// using AnyOfTypes;
using Microsoft.CodeAnalysis;
namespace ProxyInterfaceSourceGenerator.FileGenerators
@@ -30,7 +29,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
var typeSymbolAsString = typeSymbol.ToString();
var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == typeSymbolAsString);
var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.RawTypeName == typeSymbolAsString);
if (existing is not null)
{
if (!_context.ReplacedTypes.ContainsKey(typeSymbolAsString))
@@ -48,7 +47,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
foreach (var typeArgument in namedTypedSymbol.TypeArguments)
{
var typeArgumentAsString = typeArgument.ToString();
var exist = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == typeArgumentAsString);
var exist = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.RawTypeName == typeArgumentAsString);
if (exist is not null)
{
isReplaced = true;
@@ -68,13 +67,13 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
return typeSymbolAsString;
}
protected INamedTypeSymbol GetType(string name)
protected INamedTypeSymbol GetNamedTypeSymbolByFullName(string fullName)
{
// The GetTypeByMetadataName method returns null if no type matches the full name or if 2 or more types (in different assemblies) match the full name.
var symbol = _context.GeneratorExecutionContext.Compilation.GetTypeByMetadataName(name);
var symbol = _context.GeneratorExecutionContext.Compilation.GetTypeByMetadataName(fullName);
if (symbol is null)
{
throw new Exception($"The type '{name}' is not found.");
throw new Exception($"The type '{fullName}' is not found.");
}
return symbol;
@@ -1,8 +1,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using ProxyInterfaceSourceGenerator.Enums;
using ProxyInterfaceSourceGenerator.Extensions;
using ProxyInterfaceSourceGenerator.SyntaxReceiver;
using ProxyInterfaceSourceGenerator.Utils;
namespace ProxyInterfaceSourceGenerator.FileGenerators
@@ -18,72 +20,62 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
{
foreach (var ci in _context.CandidateInterfaces)
{
yield return GenerateFile(ci.Value.Namespace, ci.Value.InterfaceName, ci.Value.TypeName, ci.Value.ProxyAll);
yield return GenerateFile(ci.Value);
}
}
private FileData GenerateFile(string ns, string interfaceName, string typeName, bool proxyAll)
private FileData GenerateFile(ProxyData pd)
{
var symbol = GetType(typeName);
var targetClassSymbol = GetNamedTypeSymbolByFullName(pd.TypeName);
var interfaceName = targetClassSymbol.ResolveInterfaceNameWithOptionalTypeConstraints(pd.InterfaceName);
var file = new FileData(
$"{interfaceName}.cs",
CreatePartialInterfaceCode(ns, symbol, interfaceName, proxyAll)
$"{pd.FileName}.cs",
CreatePartialInterfaceCode(pd.Namespace, targetClassSymbol, interfaceName, pd.ProxyAll)
);
_context.GeneratedData.Add(new() { InterfaceName = interfaceName, ClassName = null, FileData = file });
// _context.GeneratedData.Add(new() { InterfaceName = interfaceName, ClassName = null, FileData = file });
return file;
}
private string CreatePartialInterfaceCode(string ns, INamedTypeSymbol symbol, string interfaceName, bool proxyAll) => $@"using System;
private string CreatePartialInterfaceCode(string ns, INamedTypeSymbol targetClassSymbol, string interfaceName, bool proxyAll) => $@"using System;
namespace {ns}
{{
public partial interface {interfaceName}
{{
{GenerateProperties(symbol, proxyAll)}
{GenerateProperties(targetClassSymbol, proxyAll)}
{GenerateMethods(symbol)}
{GenerateMethods(targetClassSymbol)}
}}
}}";
private string GenerateProperties(INamedTypeSymbol symbol, bool proxyAll)
private string GenerateProperties(INamedTypeSymbol targetClassSymbol, bool proxyAll)
{
var str = new StringBuilder();
foreach (var property in MemberHelper.GetPublicProperties(symbol))
foreach (var property in MemberHelper.GetPublicProperties(targetClassSymbol))
{
switch (property.GetTypeEnum())
var type = GetPropertyType(property, out var isReplaced);
if (isReplaced)
{
case TypeEnum.ValueTypeOrString:
case TypeEnum.Interface:
str.AppendLine($" {property.ToPropertyText()}");
str.AppendLine();
break;
default:
var type = GetPropertyType(property, out var isReplaced);
if (isReplaced)
{
str.AppendLine($" {property.ToPropertyText(type)}");
}
else
{
str.AppendLine($" {property.ToPropertyText()}");
}
str.AppendLine();
break;
str.AppendLine($" {property.ToPropertyText(type)}");
}
else
{
str.AppendLine($" {property.ToPropertyText()}");
}
str.AppendLine();
}
return str.ToString();
}
private string GenerateMethods(INamedTypeSymbol symbol)
private string GenerateMethods(INamedTypeSymbol targetClassSymbol)
{
var str = new StringBuilder();
foreach (var method in MemberHelper.GetPublicMethods(symbol))
foreach (var method in MemberHelper.GetPublicMethods(targetClassSymbol))
{
var methodParameters = new List<string>();
foreach (var ps in method.Parameters)
@@ -91,8 +83,8 @@ namespace {ns}
var type = ps.GetTypeEnum() == TypeEnum.Complex ? GetParameterType(ps, out _) : ps.Type.ToString();
methodParameters.Add($"{ps.GetParamsPrefix()}{ps.GetRefPrefix()}{type} {ps.GetSanitizedName()}{ps.GetDefaultValue()}");
}
str.AppendLine($" {GetReplacedType(method.ReturnType, out _)} {method.Name}({string.Join(", ", methodParameters)});");
str.AppendLine($" {GetReplacedType(method.ReturnType, out _)} {method.GetMethodNameWithOptionalTypeParameters()}({string.Join(", ", methodParameters)}){method.GetWhereStatement()};");
str.AppendLine();
}
@@ -5,6 +5,7 @@ using System.Text;
using Microsoft.CodeAnalysis;
using ProxyInterfaceSourceGenerator.Enums;
using ProxyInterfaceSourceGenerator.Extensions;
using ProxyInterfaceSourceGenerator.SyntaxReceiver;
using ProxyInterfaceSourceGenerator.Utils;
namespace ProxyInterfaceSourceGenerator.FileGenerators
@@ -19,38 +20,41 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
{
foreach (var ci in _context.CandidateInterfaces)
{
yield return GenerateFile(ci.Value.Namespace, ci.Value.InterfaceName, ci.Value.ClassName, ci.Value.TypeName, ci.Value.ProxyAll);
yield return GenerateFile(ci.Value); //.Namespace, ci.Value.InterfaceName, ci.Value.ClassName, ci.Value.TypeName, ci.Value.ProxyAll);
}
}
private FileData GenerateFile(string ns, string interfaceName, string className, string typeName, bool proxyAll)
private FileData GenerateFile(ProxyData pd)
{
var symbol = GetType(typeName);
var targetClassSymbol = GetNamedTypeSymbolByFullName(pd.TypeName);
var interfaceName = targetClassSymbol.ResolveInterfaceNameWithOptionalTypeConstraints(pd.InterfaceName);
var className = targetClassSymbol.ResolveProxyClassName();
var constructorName = $"{targetClassSymbol.Name}Proxy";
var file = new FileData(
$"{className}Proxy.cs",
CreateProxyClassCode(ns, symbol, interfaceName, className, proxyAll)
$"{pd.FileName}Proxy.cs",
CreateProxyClassCode(pd.Namespace, targetClassSymbol, interfaceName, className, constructorName)
);
_context.GeneratedData.Add(new() { InterfaceName = interfaceName, ClassName = className, FileData = file });
// _context.GeneratedData.Add(new() { InterfaceName = interfaceName, ClassName = pd.ClassName, FileData = file });
return file;
}
private string CreateProxyClassCode(string ns, INamedTypeSymbol symbol, string interfaceName, string className, bool proxyAll) => $@"using System;
private string CreateProxyClassCode(string ns, INamedTypeSymbol targetClassSymbol, string interfaceName, string className, string constructorName) => $@"using System;
using AutoMapper;
namespace {ns}
{{
public class {className}Proxy : {interfaceName}
public class {className} : {interfaceName}
{{
public {symbol} _Instance {{ get; }}
public {targetClassSymbol} _Instance {{ get; }}
{GeneratePublicProperties(symbol, proxyAll)}
{GeneratePublicProperties(targetClassSymbol, false)}
{GeneratePublicMethods(symbol)}
{GeneratePublicMethods(targetClassSymbol)}
public {className}Proxy({symbol} instance)
public {constructorName}({targetClassSymbol} instance)
{{
_Instance = instance;
@@ -86,43 +90,31 @@ namespace {ns}
return str.ToString();
}
private string GeneratePublicProperties(INamedTypeSymbol symbol, bool proxyAll)
private string GeneratePublicProperties(INamedTypeSymbol targetClassSymbol, bool proxyAll)
{
var str = new StringBuilder();
foreach (var property in MemberHelper.GetPublicProperties(symbol))
foreach (var property in MemberHelper.GetPublicProperties(targetClassSymbol))
{
switch (property.GetTypeEnum())
var type = GetPropertyType(property, out var isReplaced);
if (isReplaced)
{
case TypeEnum.ValueTypeOrString:
case TypeEnum.Interface:
str.AppendLine($" public {property.ToPropertyTextForClass()}");
str.AppendLine();
break;
default:
var type = GetPropertyType(property, out var isReplaced);
if (isReplaced)
{
str.AppendLine($" public {property.ToPropertyTextForClass(type)}");
}
else
{
str.AppendLine($" public {property.ToPropertyTextForClass()}");
}
str.AppendLine();
break;
str.AppendLine($" public {property.ToPropertyTextForClass(type)}");
}
else
{
str.AppendLine($" public {property.ToPropertyTextForClass()}");
}
str.AppendLine();
}
return str.ToString();
}
private string GeneratePublicMethods(INamedTypeSymbol symbol)
private string GeneratePublicMethods(INamedTypeSymbol targetClassSymbol)
{
var str = new StringBuilder();
foreach (var method in MemberHelper.GetPublicMethods(symbol))
foreach (var method in MemberHelper.GetPublicMethods(targetClassSymbol))
{
var methodParameters = new List<string>();
var invokeParameters = new List<string>();
@@ -137,7 +129,7 @@ namespace {ns}
string returnTypeAsString = GetReplacedType(method.ReturnType, out var returnIsReplaced);
str.AppendLine($" public {returnTypeAsString} {method.Name}({string.Join(", ", methodParameters)})");
str.AppendLine($" public {returnTypeAsString} {method.GetMethodNameWithOptionalTypeParameters()}({string.Join(", ", methodParameters)}){method.GetWhereStatement()}");
str.AppendLine(" {");
foreach (var ps in method.Parameters)
{
@@ -165,11 +157,11 @@ namespace {ns}
if (returnTypeAsString == "void")
{
str.AppendLine($" _Instance.{method.Name}({string.Join(", ", invokeParameters)});");
str.AppendLine($" _Instance.{method.GetMethodNameWithOptionalTypeParameters()}({string.Join(", ", invokeParameters)});");
}
else
{
str.AppendLine($" var {alternateReturnVariableName} = _Instance.{method.Name}({string.Join(", ", invokeParameters)});");
str.AppendLine($" var {alternateReturnVariableName} = _Instance.{method.GetMethodNameWithOptionalTypeParameters()}({string.Join(", ", invokeParameters)});");
}
foreach (var ps in method.Parameters.Where(p => p.RefKind == RefKind.Out))
@@ -1,9 +1,7 @@
using System.Linq;
namespace ProxyInterfaceSourceGenerator.SyntaxReceiver
namespace ProxyInterfaceSourceGenerator.SyntaxReceiver
{
internal record ProxyData(string Namespace, string InterfaceName, string TypeName, bool ProxyAll)
internal record ProxyData(string Namespace, string InterfaceName, string RawTypeName, string TypeName, bool ProxyAll)
{
public string ClassName => TypeName.Split('.').Last();
public string FileName => TypeName.Replace('.', '_').Replace('`', '_');
}
}
@@ -1,28 +1,28 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using ProxyInterfaceSourceGenerator.Extensions;
namespace ProxyInterfaceSourceGenerator.SyntaxReceiver
{
internal class ProxySyntaxReceiver : ISyntaxReceiver
{
private static readonly string[] Modifiers = new[] { "public", "partial" };
public IDictionary<InterfaceDeclarationSyntax, ProxyData> CandidateInterfaces { get; } = new Dictionary<InterfaceDeclarationSyntax, ProxyData>();
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
if (syntaxNode is InterfaceDeclarationSyntax interfaceDeclarationSyntax && TryGet(interfaceDeclarationSyntax, out var data))
{
CandidateInterfaces.Add(interfaceDeclarationSyntax, data);
}
}
private static bool TryGet(InterfaceDeclarationSyntax interfaceDeclarationSyntax, out ProxyData data)
{
data = new(string.Empty, string.Empty, string.Empty, false);
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using ProxyInterfaceSourceGenerator.Extensions;
namespace ProxyInterfaceSourceGenerator.SyntaxReceiver
{
internal class ProxySyntaxReceiver : ISyntaxReceiver
{
private static readonly string[] Modifiers = new[] { "public", "partial" };
public IDictionary<InterfaceDeclarationSyntax, ProxyData> CandidateInterfaces { get; } = new Dictionary<InterfaceDeclarationSyntax, ProxyData>();
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
if (syntaxNode is InterfaceDeclarationSyntax interfaceDeclarationSyntax && TryGet(interfaceDeclarationSyntax, out var data))
{
CandidateInterfaces.Add(interfaceDeclarationSyntax, data);
}
}
private static bool TryGet(InterfaceDeclarationSyntax interfaceDeclarationSyntax, out ProxyData data)
{
data = new(string.Empty, string.Empty, string.Empty, string.Empty, false);
if (interfaceDeclarationSyntax.Modifiers.Select(m => m.ToString()).Except(Modifiers).Count() != 0)
{
@@ -30,33 +30,43 @@ namespace ProxyInterfaceSourceGenerator.SyntaxReceiver
return false;
}
var attributeLists = interfaceDeclarationSyntax.AttributeLists.FirstOrDefault(x => x.Attributes.Any(a => a.Name.ToString().Equals("ProxyInterfaceGenerator.Proxy")));
if (attributeLists is null)
{
return false;
}
var argumentList = attributeLists.Attributes.FirstOrDefault()?.ArgumentList;
if (argumentList is null)
{
return false;
}
string ns = string.Empty;
if (SyntaxNodeUtils.TryGetParentSyntax(interfaceDeclarationSyntax, out NamespaceDeclarationSyntax namespaceDeclarationSyntax))
{
ns = namespaceDeclarationSyntax.Name.ToString();
}
data = new
(
ns,
interfaceDeclarationSyntax.Identifier.ToString(),
((TypeOfExpressionSyntax)argumentList.Arguments[0].Expression).Type.ToString(),
false //bool.Parse(argumentList.Arguments[1].Expression.GetText().ToString())
);
return true;
}
}
var attributeLists = interfaceDeclarationSyntax.AttributeLists.FirstOrDefault(x => x.Attributes.Any(a => a.Name.ToString().Equals("ProxyInterfaceGenerator.Proxy")));
if (attributeLists is null)
{
return false;
}
var argumentList = attributeLists.Attributes.FirstOrDefault()?.ArgumentList;
if (argumentList is null)
{
return false;
}
string ns = string.Empty;
if (SyntaxNodeUtils.TryGetParentSyntax(interfaceDeclarationSyntax, out NamespaceDeclarationSyntax namespaceDeclarationSyntax))
{
ns = namespaceDeclarationSyntax.Name.ToString();
}
string rawTypename = ((TypeOfExpressionSyntax)argumentList.Arguments[0].Expression).Type.ToString();
data = new
(
ns,
interfaceDeclarationSyntax.Identifier.ToString(),
rawTypename,
ResolveType(rawTypename),
false //bool.Parse(argumentList.Arguments[1].Expression.GetText().ToString())
);
return true;
}
private static string ResolveType(string typeName)
{
return !(typeName.Contains('<') && typeName.Contains('>')) ?
typeName :
$"{typeName.Replace("<", string.Empty).Replace(">", string.Empty).Replace(",", string.Empty).Trim()}`{typeName.Count(c => c == ',') + 1}";
}
}
}