66using System . Reflection ;
77using System . Runtime . Loader ;
88using System . Text ;
9- using MattSourceGenHelpers . Abstractions ;
109
1110namespace MattSourceGenHelpers . Generators ;
1211
@@ -16,6 +15,13 @@ internal sealed record SwitchBodyData(
1615
1716internal static class GeneratesMethodExecutionRuntime
1817{
18+ // The Abstraction assembly cannot be referenced directly here, even though the IDE makes it look like it could. However, the source generation process will fail if it is.
19+ private const string AbstractionsAssemblyName = "MattSourceGenHelpers.Abstractions" ;
20+ private const string GenerateTypeName = "MattSourceGenHelpers.Abstractions.Generate" ;
21+ private const string RecordingGeneratorsFactoryTypeName = "MattSourceGenHelpers.Abstractions.RecordingGeneratorsFactory" ;
22+ private const string CurrentGeneratorPropertyName = "CurrentGenerator" ;
23+ private const string LastRecordPropertyName = "LastRecord" ;
24+
1925 internal static ( string ? value , string ? error ) ExecuteSimpleGeneratorMethod (
2026 IMethodSymbol generatorMethod ,
2127 IMethodSymbol partialMethod ,
@@ -56,37 +62,37 @@ internal static (SwitchBodyData? record, string? error) ExecuteFluentGeneratorMe
5662 Path . GetFileNameWithoutExtension ( reference . FilePath ) ,
5763 assemblyName . Name ,
5864 StringComparison . OrdinalIgnoreCase ) ) ;
59- return match ? . FilePath != null ? context . LoadFromAssemblyPath ( match . FilePath ) : null ;
65+ return match ? . FilePath != null
66+ ? context . LoadFromAssemblyPath ( ResolveImplementationAssemblyPath ( match . FilePath ) )
67+ : null ;
6068 } ;
6169
6270 Assembly assembly = loadContext . LoadFromStream ( stream ) ;
6371
64- string abstractionAssemblyName = typeof ( Generate ) . Assembly . GetName ( ) . Name ! ;
65-
6672 PortableExecutableReference ? abstractionsReference = compilation . References
6773 . OfType < PortableExecutableReference > ( )
6874 . FirstOrDefault ( reference => reference . FilePath is not null && string . Equals (
6975 Path . GetFileNameWithoutExtension ( reference . FilePath ) ,
70- abstractionAssemblyName ,
76+ AbstractionsAssemblyName ,
7177 StringComparison . OrdinalIgnoreCase ) ) ;
7278
7379 if ( abstractionsReference ? . FilePath == null )
7480 {
75- return ( null , $ "Could not find { abstractionAssemblyName } reference in compilation") ;
81+ return ( null , $ "Could not find { AbstractionsAssemblyName } reference in compilation") ;
7682 }
7783
7884 string abstractionsAssemblyPath = ResolveImplementationAssemblyPath ( abstractionsReference . FilePath ) ;
7985 Assembly abstractionsAssembly = loadContext . LoadFromAssemblyPath ( abstractionsAssemblyPath ) ;
8086
81- Type ? generatorStaticType = abstractionsAssembly . GetType ( typeof ( Generate ) . FullName ! ) ;
82- Type ? recordingFactoryType = abstractionsAssembly . GetType ( typeof ( RecordingGeneratorsFactory ) . FullName ! ) ;
87+ Type ? generatorStaticType = abstractionsAssembly . GetType ( GenerateTypeName ) ;
88+ Type ? recordingFactoryType = abstractionsAssembly . GetType ( RecordingGeneratorsFactoryTypeName ) ;
8389 if ( generatorStaticType == null || recordingFactoryType == null )
8490 {
85- return ( null , $ "Could not find { typeof ( Generate ) . FullName } or { typeof ( RecordingGeneratorsFactory ) . FullName } types in Abstractions assembly") ;
91+ return ( null , $ "Could not find { GenerateTypeName } or { RecordingGeneratorsFactoryTypeName } types in Abstractions assembly") ;
8692 }
8793
8894 object ? recordingFactory = Activator . CreateInstance ( recordingFactoryType ) ;
89- PropertyInfo ? currentGeneratorProperty = generatorStaticType . GetProperty ( nameof ( Generate . CurrentGenerator ) , BindingFlags . Public | BindingFlags . Static ) ;
95+ PropertyInfo ? currentGeneratorProperty = generatorStaticType . GetProperty ( CurrentGeneratorPropertyName , BindingFlags . Public | BindingFlags . Static ) ;
9096 currentGeneratorProperty ? . SetValue ( null , recordingFactory ) ;
9197
9298 string typeName = generatorMethod . ContainingType . ToDisplayString ( ) ;
@@ -104,7 +110,7 @@ internal static (SwitchBodyData? record, string? error) ExecuteFluentGeneratorMe
104110
105111 generatorMethodInfo . Invoke ( null , null ) ;
106112
107- PropertyInfo ? lastRecordProperty = recordingFactoryType . GetProperty ( nameof ( RecordingGeneratorsFactory . LastRecord ) ) ;
113+ PropertyInfo ? lastRecordProperty = recordingFactoryType . GetProperty ( LastRecordPropertyName ) ;
108114 object ? lastRecord = lastRecordProperty ? . GetValue ( recordingFactory ) ;
109115 if ( lastRecord == null )
110116 {
@@ -154,7 +160,9 @@ internal static (string? value, string? error) ExecuteGeneratorMethodWithArgs(
154160 Path . GetFileNameWithoutExtension ( reference . FilePath ) ,
155161 assemblyName . Name ,
156162 StringComparison . OrdinalIgnoreCase ) ) ;
157- return match ? . FilePath != null ? context . LoadFromAssemblyPath ( match . FilePath ) : null ;
163+ return match ? . FilePath != null
164+ ? context . LoadFromAssemblyPath ( ResolveImplementationAssemblyPath ( match . FilePath ) )
165+ : null ;
158166 } ;
159167
160168 Assembly assembly = loadContext . LoadFromStream ( stream ) ;
0 commit comments