File tree Expand file tree Collapse file tree 3 files changed +35
-2
lines changed
InterfaceStubGenerator.Shared Expand file tree Collapse file tree 3 files changed +35
-2
lines changed Original file line number Diff line number Diff line change 2121 <Compile Include =" $(MSBuildThisFileDirectory)Models\MethodModel.cs" />
2222 <Compile Include =" $(MSBuildThisFileDirectory)Models\ParameterModel.cs" />
2323 <Compile Include =" $(MSBuildThisFileDirectory)Models\TypeConstraint.cs" />
24+ <Compile Include =" $(MSBuildThisFileDirectory)Models\WellKnownTYpes.cs" />
2425 <Compile Include =" $(MSBuildThisFileDirectory)Parser.cs" />
2526 <Compile Include =" $(MSBuildThisFileDirectory)UniqueNameBuilder.cs" />
2627 </ItemGroup >
Original file line number Diff line number Diff line change 1+ using Microsoft . CodeAnalysis ;
2+
3+ namespace Refit . Generator ;
4+
5+ public class WellKnownTypes ( Compilation compilation )
6+ {
7+ readonly Dictionary < string , INamedTypeSymbol ? > cachedTypes = new ( ) ;
8+
9+ public INamedTypeSymbol Get < T > ( ) => Get ( typeof ( T ) ) ;
10+ public INamedTypeSymbol Get ( Type type )
11+ {
12+ return Get ( type . FullName ?? throw new InvalidOperationException ( "Could not get name of type " + type ) ) ;
13+ }
14+
15+ public INamedTypeSymbol ? TryGet ( string typeFullName )
16+ {
17+ if ( cachedTypes . TryGetValue ( typeFullName , out var typeSymbol ) )
18+ {
19+ return typeSymbol ;
20+ }
21+
22+ typeSymbol = compilation . GetTypeByMetadataName ( typeFullName ) ;
23+ cachedTypes . Add ( typeFullName , typeSymbol ) ;
24+
25+ return typeSymbol ;
26+ }
27+
28+ INamedTypeSymbol Get ( string typeFullName ) =>
29+ TryGet ( typeFullName ) ?? throw new InvalidOperationException ( "Could not get type " + typeFullName ) ;
30+ }
Original file line number Diff line number Diff line change @@ -32,6 +32,8 @@ CancellationToken cancellationToken
3232 if ( compilation == null )
3333 throw new ArgumentNullException ( nameof ( compilation ) ) ;
3434
35+ var wellKnownTypes = new WellKnownTypes ( compilation ) ;
36+
3537 refitInternalNamespace = $ "{ refitInternalNamespace ?? string . Empty } RefitInternalGenerated";
3638
3739 // Remove - as they are valid in csproj, but invalid in a namespace
@@ -41,8 +43,8 @@ CancellationToken cancellationToken
4143 // TODO: we should allow source generators to provide source during initialize, so that this step isn't required.
4244 var options = ( CSharpParseOptions ) compilation . SyntaxTrees [ 0 ] . Options ;
4345
44- var disposableInterfaceSymbol = compilation . GetTypeByMetadataName ( "System. IDisposable" ) ! ;
45- var httpMethodBaseAttributeSymbol = compilation . GetTypeByMetadataName (
46+ var disposableInterfaceSymbol = wellKnownTypes . Get ( typeof ( IDisposable ) ) ;
47+ var httpMethodBaseAttributeSymbol = wellKnownTypes . TryGet (
4648 "Refit.HttpMethodAttribute"
4749 ) ;
4850
You can’t perform that action at this time.
0 commit comments