@@ -11,17 +11,17 @@ namespace DotNetApiDiff.AssemblyLoading;
1111/// </summary>
1212public class AssemblyLoader : IAssemblyLoader , IDisposable
1313{
14- private readonly ILogger < AssemblyLoader > _logger ;
15- private readonly Dictionary < string , Assembly > _loadedAssemblies = new ( ) ;
16- private readonly Dictionary < string , IsolatedAssemblyLoadContext > _loadContexts = new ( ) ;
14+ private readonly ILogger < AssemblyLoader > logger ;
15+ private readonly Dictionary < string , Assembly > loadedAssemblies = new Dictionary < string , Assembly > ( ) ;
16+ private readonly Dictionary < string , IsolatedAssemblyLoadContext > loadContexts = new Dictionary < string , IsolatedAssemblyLoadContext > ( ) ;
1717
1818 /// <summary>
1919 /// Creates a new assembly loader with the specified logger
2020 /// </summary>
2121 /// <param name="logger">Logger for diagnostic information</param>
2222 public AssemblyLoader ( ILogger < AssemblyLoader > logger )
2323 {
24- _logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
24+ this . logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
2525 }
2626
2727 /// <summary>
@@ -39,33 +39,33 @@ public Assembly LoadAssembly(string assemblyPath)
3939 {
4040 if ( string . IsNullOrWhiteSpace ( assemblyPath ) )
4141 {
42- _logger . LogError ( "Assembly path cannot be null or empty" ) ;
42+ this . logger . LogError ( "Assembly path cannot be null or empty" ) ;
4343 throw new ArgumentException ( "Assembly path cannot be null or empty" , nameof ( assemblyPath ) ) ;
4444 }
4545
4646 // Normalize the path to ensure consistent dictionary keys
4747 assemblyPath = Path . GetFullPath ( assemblyPath ) ;
4848
4949 // Check if we've already loaded this assembly
50- if ( _loadedAssemblies . TryGetValue ( assemblyPath , out var loadedAssembly ) )
50+ if ( this . loadedAssemblies . TryGetValue ( assemblyPath , out var loadedAssembly ) )
5151 {
52- _logger . LogDebug ( "Returning previously loaded assembly from {Path}" , assemblyPath ) ;
52+ this . logger . LogDebug ( "Returning previously loaded assembly from {Path}" , assemblyPath ) ;
5353 return loadedAssembly ;
5454 }
5555
56- _logger . LogInformation ( "Loading assembly from {Path}" , assemblyPath ) ;
56+ this . logger . LogInformation ( "Loading assembly from {Path}" , assemblyPath ) ;
5757
5858 try
5959 {
6060 // Verify the file exists
6161 if ( ! File . Exists ( assemblyPath ) )
6262 {
63- _logger . LogError ( "Assembly file not found: {Path}" , assemblyPath ) ;
63+ this . logger . LogError ( "Assembly file not found: {Path}" , assemblyPath ) ;
6464 throw new FileNotFoundException ( $ "Assembly file not found: { assemblyPath } ", assemblyPath ) ;
6565 }
6666
6767 // Create a new isolated load context for this assembly
68- var loadContext = new IsolatedAssemblyLoadContext ( assemblyPath , _logger ) ;
68+ var loadContext = new IsolatedAssemblyLoadContext ( assemblyPath , this . logger ) ;
6969
7070 // Add the directory of the assembly as a search path
7171 var assemblyDirectory = Path . GetDirectoryName ( assemblyPath ) ;
@@ -78,37 +78,39 @@ public Assembly LoadAssembly(string assemblyPath)
7878 var assembly = loadContext . LoadFromAssemblyPath ( assemblyPath ) ;
7979
8080 // Store the assembly and load context for later use
81- _loadedAssemblies [ assemblyPath ] = assembly ;
82- _loadContexts [ assemblyPath ] = loadContext ;
81+ this . loadedAssemblies [ assemblyPath ] = assembly ;
82+ this . loadContexts [ assemblyPath ] = loadContext ;
8383
84- _logger . LogInformation ( "Successfully loaded assembly: {AssemblyName} from {Path}" ,
85- assembly . GetName ( ) . Name , assemblyPath ) ;
84+ this . logger . LogInformation (
85+ "Successfully loaded assembly: {AssemblyName} from {Path}" ,
86+ assembly . GetName ( ) . Name ,
87+ assemblyPath ) ;
8688
8789 return assembly ;
8890 }
8991 catch ( FileNotFoundException ex )
9092 {
91- _logger . LogError ( ex , "Assembly file not found: {Path}" , assemblyPath ) ;
93+ this . logger . LogError ( ex , "Assembly file not found: {Path}" , assemblyPath ) ;
9294 throw ;
9395 }
9496 catch ( BadImageFormatException ex )
9597 {
96- _logger . LogError ( ex , "Invalid assembly format: {Path}" , assemblyPath ) ;
98+ this . logger . LogError ( ex , "Invalid assembly format: {Path}" , assemblyPath ) ;
9799 throw ;
98100 }
99101 catch ( SecurityException ex )
100102 {
101- _logger . LogError ( ex , "Security exception loading assembly: {Path}" , assemblyPath ) ;
103+ this . logger . LogError ( ex , "Security exception loading assembly: {Path}" , assemblyPath ) ;
102104 throw ;
103105 }
104106 catch ( PathTooLongException ex )
105107 {
106- _logger . LogError ( ex , "Path too long for assembly: {Path}" , assemblyPath ) ;
108+ this . logger . LogError ( ex , "Path too long for assembly: {Path}" , assemblyPath ) ;
107109 throw ;
108110 }
109111 catch ( ReflectionTypeLoadException ex )
110112 {
111- _logger . LogError ( ex , "Failed to load types from assembly: {Path}" , assemblyPath ) ;
113+ this . logger . LogError ( ex , "Failed to load types from assembly: {Path}" , assemblyPath ) ;
112114
113115 // Log the loader exceptions for more detailed diagnostics
114116 if ( ex . LoaderExceptions != null )
@@ -117,7 +119,7 @@ public Assembly LoadAssembly(string assemblyPath)
117119 {
118120 if ( loaderEx != null )
119121 {
120- _logger . LogError ( loaderEx , "Loader exception: {Message}" , loaderEx . Message ) ;
122+ this . logger . LogError ( loaderEx , "Loader exception: {Message}" , loaderEx . Message ) ;
121123 }
122124 }
123125 }
@@ -126,7 +128,7 @@ public Assembly LoadAssembly(string assemblyPath)
126128 }
127129 catch ( Exception ex )
128130 {
129- _logger . LogError ( ex , "Unexpected error loading assembly: {Path}" , assemblyPath ) ;
131+ this . logger . LogError ( ex , "Unexpected error loading assembly: {Path}" , assemblyPath ) ;
130132 throw ;
131133 }
132134 }
@@ -140,7 +142,7 @@ public bool IsValidAssembly(string assemblyPath)
140142 {
141143 if ( string . IsNullOrWhiteSpace ( assemblyPath ) )
142144 {
143- _logger . LogDebug ( "Assembly path is null or empty" ) ;
145+ this . logger . LogDebug ( "Assembly path is null or empty" ) ;
144146 return false ;
145147 }
146148
@@ -149,7 +151,7 @@ public bool IsValidAssembly(string assemblyPath)
149151 // Check if the file exists
150152 if ( ! File . Exists ( assemblyPath ) )
151153 {
152- _logger . LogDebug ( "Assembly file does not exist: {Path}" , assemblyPath ) ;
154+ this . logger . LogDebug ( "Assembly file does not exist: {Path}" , assemblyPath ) ;
153155 return false ;
154156 }
155157
@@ -160,7 +162,7 @@ public bool IsValidAssembly(string assemblyPath)
160162 var assembly = tempContext . LoadFromAssemblyPath ( assemblyPath ) ;
161163
162164 // If we got here, the assembly is valid
163- _logger . LogDebug ( "Successfully validated assembly: {Path}" , assemblyPath ) ;
165+ this . logger . LogDebug ( "Successfully validated assembly: {Path}" , assemblyPath ) ;
164166 return true ;
165167 }
166168 finally
@@ -171,7 +173,7 @@ public bool IsValidAssembly(string assemblyPath)
171173 }
172174 catch ( Exception ex )
173175 {
174- _logger . LogDebug ( ex , "Assembly validation failed for {Path}: {Message}" , assemblyPath , ex . Message ) ;
176+ this . logger . LogDebug ( ex , "Assembly validation failed for {Path}: {Message}" , assemblyPath , ex . Message ) ;
175177 return false ;
176178 }
177179 }
@@ -181,30 +183,30 @@ public bool IsValidAssembly(string assemblyPath)
181183 /// </summary>
182184 public void UnloadAll ( )
183185 {
184- _logger . LogInformation ( "Unloading all assemblies" ) ;
186+ this . logger . LogInformation ( "Unloading all assemblies" ) ;
185187
186- foreach ( var context in _loadContexts . Values )
188+ foreach ( var context in this . loadContexts . Values )
187189 {
188190 try
189191 {
190192 context . Unload ( ) ;
191193 }
192194 catch ( Exception ex )
193195 {
194- _logger . LogWarning ( ex , "Error unloading assembly context" ) ;
196+ this . logger . LogWarning ( ex , "Error unloading assembly context" ) ;
195197 }
196198 }
197199
198- _loadContexts . Clear ( ) ;
199- _loadedAssemblies . Clear ( ) ;
200+ this . loadContexts . Clear ( ) ;
201+ this . loadedAssemblies . Clear ( ) ;
200202 }
201203
202204 /// <summary>
203205 /// Disposes the assembly loader and unloads all assemblies
204206 /// </summary>
205207 public void Dispose ( )
206208 {
207- _logger . LogDebug ( "Disposing AssemblyLoader" ) ;
209+ this . logger . LogDebug ( "Disposing AssemblyLoader" ) ;
208210 UnloadAll ( ) ;
209211 GC . SuppressFinalize ( this ) ;
210212 }
0 commit comments