Add support for using a simple type-name (#22)

This commit is contained in:
Stef Heyenrath
2021-08-02 11:49:41 +02:00
committed by GitHub
parent a778c35728
commit 7baf050c12
10 changed files with 75 additions and 23 deletions
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
@@ -67,16 +68,28 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
return typeSymbolAsString;
}
protected INamedTypeSymbol GetNamedTypeSymbolByFullName(string fullName)
protected INamedTypeSymbol GetNamedTypeSymbolByFullName(string name, IEnumerable<string>? usings = null)
{
// 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(fullName);
if (symbol is null)
{
throw new Exception($"The type '{fullName}' is not found.");
var symbol = _context.GeneratorExecutionContext.Compilation.GetTypeByMetadataName(name);
if (symbol is not null)
{
return symbol;
}
if (usings is not null)
{
foreach (var @using in usings)
{
symbol = _context.GeneratorExecutionContext.Compilation.GetTypeByMetadataName($"{@using}.{name}");
if (symbol is not null)
{
return symbol;
}
}
}
return symbol;
throw new Exception($"The type '{name}' is not found.");
}
}
}
@@ -26,7 +26,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
private FileData GenerateFile(ProxyData pd)
{
var targetClassSymbol = GetNamedTypeSymbolByFullName(pd.TypeName);
var targetClassSymbol = GetNamedTypeSymbolByFullName(pd.TypeName, pd.Usings);
var interfaceName = targetClassSymbol.ResolveInterfaceNameWithOptionalTypeConstraints(pd.InterfaceName);
var file = new FileData(
@@ -26,7 +26,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
private FileData GenerateFile(ProxyData pd)
{
var targetClassSymbol = GetNamedTypeSymbolByFullName(pd.TypeName);
var targetClassSymbol = GetNamedTypeSymbolByFullName(pd.TypeName, pd.Usings);
var interfaceName = targetClassSymbol.ResolveInterfaceNameWithOptionalTypeConstraints(pd.InterfaceName);
var className = targetClassSymbol.ResolveProxyClassName();
var constructorName = $"{targetClassSymbol.Name}Proxy";