Add support for using a simple type-name (#22)
This commit is contained in:
@@ -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";
|
||||
|
||||
Reference in New Issue
Block a user