@@ -56,61 +56,103 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
|
||||
|
||||
protected string GetPropertyType(IPropertySymbol property)
|
||||
{
|
||||
var propertyTypeAsString = property.Type.ToString();
|
||||
return GetReplacedType(property.Type);
|
||||
|
||||
var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == propertyTypeAsString);
|
||||
if (existing is not null)
|
||||
{
|
||||
if (!_context.ReplacedTypes.ContainsKey(propertyTypeAsString))
|
||||
{
|
||||
_context.ReplacedTypes.Add(propertyTypeAsString, existing.InterfaceName);
|
||||
}
|
||||
//var propertyTypeAsString = property.Type.ToString();
|
||||
|
||||
return existing.InterfaceName;
|
||||
}
|
||||
//var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == propertyTypeAsString);
|
||||
//if (existing is not null)
|
||||
//{
|
||||
// if (!_context.ReplacedTypes.ContainsKey(propertyTypeAsString))
|
||||
// {
|
||||
// _context.ReplacedTypes.Add(propertyTypeAsString, existing.InterfaceName);
|
||||
// }
|
||||
|
||||
if (property.Type is INamedTypeSymbol namedTypedSymbol)
|
||||
{
|
||||
var propertyTypeAsStringToBeModified = propertyTypeAsString;
|
||||
foreach (var typeArgument in namedTypedSymbol.TypeArguments)
|
||||
{
|
||||
var typeArgumentAsString = typeArgument.ToString();
|
||||
var exist = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == typeArgumentAsString);
|
||||
if (exist is not null)
|
||||
{
|
||||
if (!_context.ReplacedTypes.ContainsKey(typeArgumentAsString))
|
||||
{
|
||||
_context.ReplacedTypes.Add(typeArgumentAsString, exist.InterfaceName);
|
||||
}
|
||||
// return existing.InterfaceName;
|
||||
//}
|
||||
|
||||
propertyTypeAsStringToBeModified = propertyTypeAsStringToBeModified.Replace(typeArgumentAsString, exist.InterfaceName);
|
||||
}
|
||||
}
|
||||
//if (property.Type is INamedTypeSymbol namedTypedSymbol)
|
||||
//{
|
||||
// var propertyTypeAsStringToBeModified = propertyTypeAsString;
|
||||
// foreach (var typeArgument in namedTypedSymbol.TypeArguments)
|
||||
// {
|
||||
// var typeArgumentAsString = typeArgument.ToString();
|
||||
// var exist = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == typeArgumentAsString);
|
||||
// if (exist is not null)
|
||||
// {
|
||||
// if (!_context.ReplacedTypes.ContainsKey(typeArgumentAsString))
|
||||
// {
|
||||
// _context.ReplacedTypes.Add(typeArgumentAsString, exist.InterfaceName);
|
||||
// }
|
||||
|
||||
return propertyTypeAsStringToBeModified;
|
||||
}
|
||||
// propertyTypeAsStringToBeModified = propertyTypeAsStringToBeModified.Replace(typeArgumentAsString, exist.InterfaceName);
|
||||
// }
|
||||
// }
|
||||
|
||||
return propertyTypeAsString;
|
||||
// return propertyTypeAsStringToBeModified;
|
||||
//}
|
||||
|
||||
//return propertyTypeAsString;
|
||||
}
|
||||
|
||||
protected string GetParameterType(IParameterSymbol property)
|
||||
{
|
||||
var propertyTypeAsString = property.Type.ToString();
|
||||
return GetReplacedType(property.Type);
|
||||
//var propertyTypeAsString = property.Type.ToString();
|
||||
|
||||
var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == propertyTypeAsString);
|
||||
//var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == propertyTypeAsString);
|
||||
//if (existing is not null)
|
||||
//{
|
||||
// if (!_context.ReplacedTypes.ContainsKey(propertyTypeAsString))
|
||||
// {
|
||||
// _context.ReplacedTypes.Add(propertyTypeAsString, existing.InterfaceName);
|
||||
// }
|
||||
|
||||
// return existing.InterfaceName;
|
||||
//}
|
||||
|
||||
//if (property.Type is INamedTypeSymbol namedTypedSymbol)
|
||||
//{
|
||||
// var propertyTypeAsStringToBeModified = propertyTypeAsString;
|
||||
// foreach (var typeArgument in namedTypedSymbol.TypeArguments)
|
||||
// {
|
||||
// var typeArgumentAsString = typeArgument.ToString();
|
||||
// var exist = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == typeArgumentAsString);
|
||||
// if (exist is not null)
|
||||
// {
|
||||
// if (!_context.ReplacedTypes.ContainsKey(typeArgumentAsString))
|
||||
// {
|
||||
// _context.ReplacedTypes.Add(typeArgumentAsString, exist.InterfaceName);
|
||||
// }
|
||||
|
||||
// propertyTypeAsStringToBeModified = propertyTypeAsStringToBeModified.Replace(typeArgumentAsString, exist.InterfaceName);
|
||||
// }
|
||||
// }
|
||||
|
||||
// return propertyTypeAsStringToBeModified;
|
||||
//}
|
||||
|
||||
//return propertyTypeAsString;
|
||||
}
|
||||
|
||||
protected string GetReplacedType(ITypeSymbol property)
|
||||
{
|
||||
var typeSymbolAsString = property.ToString();
|
||||
|
||||
var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == typeSymbolAsString);
|
||||
if (existing is not null)
|
||||
{
|
||||
if (!_context.ReplacedTypes.ContainsKey(propertyTypeAsString))
|
||||
if (!_context.ReplacedTypes.ContainsKey(typeSymbolAsString))
|
||||
{
|
||||
_context.ReplacedTypes.Add(propertyTypeAsString, existing.InterfaceName);
|
||||
_context.ReplacedTypes.Add(typeSymbolAsString, existing.InterfaceName);
|
||||
}
|
||||
|
||||
return existing.InterfaceName;
|
||||
}
|
||||
|
||||
if (property.Type is INamedTypeSymbol namedTypedSymbol)
|
||||
if (property is INamedTypeSymbol namedTypedSymbol)
|
||||
{
|
||||
var propertyTypeAsStringToBeModified = propertyTypeAsString;
|
||||
var propertyTypeAsStringToBeModified = typeSymbolAsString;
|
||||
foreach (var typeArgument in namedTypedSymbol.TypeArguments)
|
||||
{
|
||||
var typeArgumentAsString = typeArgument.ToString();
|
||||
@@ -129,7 +171,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
|
||||
return propertyTypeAsStringToBeModified;
|
||||
}
|
||||
|
||||
return propertyTypeAsString;
|
||||
return typeSymbolAsString;
|
||||
}
|
||||
|
||||
protected INamedTypeSymbol GetType(string name)
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using ProxyInterfaceSourceGenerator.Enums;
|
||||
@@ -19,17 +18,17 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
|
||||
{
|
||||
foreach (var ci in _context.CandidateInterfaces)
|
||||
{
|
||||
yield return GenerateFile(ci.Value.InterfaceName, ci.Value.TypeName, ci.Value.ProxyAll);
|
||||
yield return GenerateFile(ci.Value.Namespace, ci.Value.InterfaceName, ci.Value.TypeName, ci.Value.ProxyAll);
|
||||
}
|
||||
}
|
||||
|
||||
private FileData GenerateFile(string interfaceName, string typeName, bool proxyAll)
|
||||
private FileData GenerateFile(string ns, string interfaceName, string typeName, bool proxyAll)
|
||||
{
|
||||
var symbol = GetType(typeName);
|
||||
|
||||
var file = new FileData(
|
||||
$"{interfaceName}.cs",
|
||||
CreatePartialInterfaceCode(symbol, interfaceName, proxyAll)
|
||||
CreatePartialInterfaceCode(ns, symbol, interfaceName, proxyAll)
|
||||
);
|
||||
|
||||
_context.GeneratedData.Add(new() { InterfaceName = interfaceName, ClassName = null, FileData = file });
|
||||
@@ -37,9 +36,9 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
|
||||
return file;
|
||||
}
|
||||
|
||||
private string CreatePartialInterfaceCode(INamedTypeSymbol symbol, string interfaceName, bool proxyAll) => $@"using System;
|
||||
private string CreatePartialInterfaceCode(string ns, INamedTypeSymbol symbol, string interfaceName, bool proxyAll) => $@"using System;
|
||||
|
||||
namespace {symbol.ContainingNamespace}
|
||||
namespace {ns}
|
||||
{{
|
||||
public partial interface {interfaceName}
|
||||
{{
|
||||
@@ -97,11 +96,8 @@ namespace {symbol.ContainingNamespace}
|
||||
}
|
||||
}
|
||||
|
||||
str.AppendLine($" {method.ReturnType} {method.Name}({string.Join(", ", methodParameters)});");
|
||||
str.AppendLine($" {GetReplacedType(method.ReturnType)} {method.Name}({string.Join(", ", methodParameters)});");
|
||||
str.AppendLine();
|
||||
|
||||
//str.AppendLine($" {method.ToMethodText()};");
|
||||
//str.AppendLine();
|
||||
}
|
||||
|
||||
return str.ToString();
|
||||
|
||||
@@ -17,17 +17,17 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
|
||||
{
|
||||
foreach (var ci in _context.CandidateInterfaces)
|
||||
{
|
||||
yield return GenerateFile(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 interfaceName, string className, string typeName, bool proxyAll)
|
||||
private FileData GenerateFile(string ns, string interfaceName, string className, string typeName, bool proxyAll)
|
||||
{
|
||||
var symbol = GetType(typeName);
|
||||
|
||||
var file = new FileData(
|
||||
$"{className}Proxy.cs",
|
||||
CreateProxyClassCode(symbol, interfaceName, className, proxyAll)
|
||||
CreateProxyClassCode(ns, symbol, interfaceName, className, proxyAll)
|
||||
);
|
||||
|
||||
_context.GeneratedData.Add(new() { InterfaceName = interfaceName, ClassName = className, FileData = file });
|
||||
@@ -35,22 +35,22 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
|
||||
return file;
|
||||
}
|
||||
|
||||
private string CreateProxyClassCode(INamedTypeSymbol symbol, string interfaceName, string className, bool proxyAll) => $@"using System;
|
||||
private string CreateProxyClassCode(string ns, INamedTypeSymbol symbol, string interfaceName, string className, bool proxyAll) => $@"using System;
|
||||
using AutoMapper;
|
||||
|
||||
namespace {symbol.ContainingNamespace}
|
||||
namespace {ns}
|
||||
{{
|
||||
public class {className}Proxy : {interfaceName}
|
||||
{{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IMapper? _mapper;
|
||||
|
||||
public {className} _Instance {{ get; }}
|
||||
public {symbol} _Instance {{ get; }}
|
||||
|
||||
{GeneratePublicProperties(symbol, proxyAll)}
|
||||
|
||||
{GeneratePublicMethods(symbol)}
|
||||
|
||||
public {className}Proxy({className} instance)
|
||||
public {className}Proxy({symbol} instance)
|
||||
{{
|
||||
_Instance = instance;
|
||||
|
||||
@@ -61,6 +61,11 @@ namespace {symbol.ContainingNamespace}
|
||||
|
||||
private string GenerateAutoMapper()
|
||||
{
|
||||
if (_context.ReplacedTypes.Count == 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var str = new StringBuilder();
|
||||
|
||||
str.AppendLine(" _mapper = new MapperConfiguration(cfg =>");
|
||||
@@ -110,33 +115,38 @@ namespace {symbol.ContainingNamespace}
|
||||
foreach (var method in MemberHelper.GetPublicMethods(symbol))
|
||||
{
|
||||
var methodParameters = new List<string>();
|
||||
foreach (var ps in method.Parameters)
|
||||
{
|
||||
if (ps.GetTypeEnum() == TypeEnum.Complex)
|
||||
{
|
||||
var type = GetParameterType(ps);
|
||||
methodParameters.Add($"{type} {ps.Name}");
|
||||
}
|
||||
else
|
||||
{
|
||||
methodParameters.Add($"{ps.Type} {ps.Name}");
|
||||
}
|
||||
}
|
||||
|
||||
var invokeParameters = new List<string>();
|
||||
|
||||
foreach (var ps in method.Parameters)
|
||||
{
|
||||
if (ps.GetTypeEnum() == TypeEnum.Complex)
|
||||
{
|
||||
methodParameters.Add($"{GetParameterType(ps)} {ps.Name}");
|
||||
|
||||
invokeParameters.Add($"_mapper.Map<{ps.Type}>({ps.Name})");
|
||||
}
|
||||
else
|
||||
{
|
||||
methodParameters.Add($"{ps.Type} {ps.Name}");
|
||||
|
||||
invokeParameters.Add($"{ps.Name}");
|
||||
}
|
||||
}
|
||||
|
||||
str.AppendLine($" public {method.ReturnType} {method.Name}({string.Join(", ", methodParameters)}) => _Instance.{method.Name}({string.Join(", ", invokeParameters)});");
|
||||
string returnTypeAsString;
|
||||
string call;
|
||||
if (method.ReturnType.GetTypeEnum() == TypeEnum.Complex)
|
||||
{
|
||||
returnTypeAsString = GetReplacedType(method.ReturnType);
|
||||
call = $"_mapper.Map<{returnTypeAsString}>(_Instance.{method.Name}({string.Join(", ", invokeParameters)}))";
|
||||
}
|
||||
else
|
||||
{
|
||||
returnTypeAsString = method.ReturnType.ToString();
|
||||
call = $"_Instance.{method.Name}({string.Join(", ", invokeParameters)})";
|
||||
}
|
||||
|
||||
str.AppendLine($" public {returnTypeAsString} {method.Name}({string.Join(", ", methodParameters)}) => {call};");
|
||||
str.AppendLine();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user