|
3 | 3 | using System; |
4 | 4 | using System.Linq; |
5 | 5 | using System.IO; |
| 6 | +using System.Reflection; |
6 | 7 | using Microsoft.Extensions.DependencyInjection; |
7 | 8 | using Microsoft.Extensions.Logging; |
8 | 9 | using Microsoft.Extensions.Logging.Console; |
@@ -175,7 +176,7 @@ public void UsingAssembly_FromStream(bool loadIntoReflectionOnlyContext, string |
175 | 176 | [TestCase(true, "net7.0")] |
176 | 177 | [TestCase(false, "net7.0")] |
177 | 178 | #endif |
178 | | - public void UsingAssembly_ResolveDependency(bool loadIntoReflectionOnlyContext, string targetFrameworkMoniker) |
| 179 | + public void UsingAssembly_ResolveDependency_ProjectReference(bool loadIntoReflectionOnlyContext, string targetFrameworkMoniker) |
179 | 180 | { |
180 | 181 | var assemblyFile = new FileInfo( |
181 | 182 | TestAssemblyInfo.TestAssemblyPaths.First(f => f.Contains(targetFrameworkMoniker) && f.Contains("LibB.dll")) |
@@ -236,7 +237,7 @@ public void UsingAssembly_ResolveDependency(bool loadIntoReflectionOnlyContext, |
236 | 237 | [TestCase(true, "net7.0")] |
237 | 238 | [TestCase(false, "net7.0")] |
238 | 239 | #endif |
239 | | - public void UsingAssembly_FromStream_ResolveDependency(bool loadIntoReflectionOnlyContext, string targetFrameworkMoniker) |
| 240 | + public void UsingAssembly_FromStream_ResolveDependency_ProjectReference(bool loadIntoReflectionOnlyContext, string targetFrameworkMoniker) |
240 | 241 | { |
241 | 242 | var assemblyFile = new FileInfo( |
242 | 243 | TestAssemblyInfo.TestAssemblyPaths.First(f => f.Contains(targetFrameworkMoniker) && f.Contains("LibB.dll")) |
@@ -286,6 +287,73 @@ public void UsingAssembly_FromStream_ResolveDependency(bool loadIntoReflectionOn |
286 | 287 | GC.WaitForPendingFinalizers(); |
287 | 288 | } |
288 | 289 |
|
| 290 | + Assert.IsTrue(unloaded, nameof(unloaded)); |
| 291 | + } |
| 292 | + |
| 293 | +#if NETCOREAPP3_1_OR_GREATER || NET6_0_OR_GREATER |
| 294 | + [TestCase(true, "netstandard2.1")] |
| 295 | + [TestCase(false, "netstandard2.1")] |
| 296 | +#endif |
| 297 | +#if NET6_0_OR_GREATER |
| 298 | + [TestCase(true, "net6.0")] |
| 299 | + [TestCase(false, "net6.0")] |
| 300 | +#endif |
| 301 | +#if NET7_0_OR_GREATER |
| 302 | + [TestCase(true, "net7.0")] |
| 303 | + [TestCase(false, "net7.0")] |
| 304 | +#endif |
| 305 | + public void UsingAssembly_ResolveDependency_PackageReference(bool loadIntoReflectionOnlyContext, string targetFrameworkMoniker) |
| 306 | + { |
| 307 | + var assemblyFile = new FileInfo( |
| 308 | + TestAssemblyInfo.TestAssemblyPaths.First(f => f.Contains(targetFrameworkMoniker) && f.Contains("LibPackageReferences1.dll")) |
| 309 | + ); |
| 310 | + |
| 311 | + var result = AssemblyLoader.UsingAssembly( |
| 312 | + assemblyFile, |
| 313 | + loadIntoReflectionOnlyContext: loadIntoReflectionOnlyContext, |
| 314 | + arg: assemblyFile, |
| 315 | + (assm, arg) => { |
| 316 | + Assert.AreSame(arg, assemblyFile, nameof(arg)); |
| 317 | + |
| 318 | + Assert.IsNotNull(assm, nameof(assm)); |
| 319 | + Assert.AreEqual(arg.FullName, assm.Location, nameof(assm.Location)); |
| 320 | + |
| 321 | + Assert.DoesNotThrow(() => assm.GetExportedTypes(), nameof(assm.GetExportedTypes)); |
| 322 | + |
| 323 | + return assm |
| 324 | + .GetType("C") |
| 325 | + ?.GetMethod("M", BindingFlags.Public | BindingFlags.Static) |
| 326 | + ?.GetParameters() |
| 327 | + ?[0] |
| 328 | + ?.ParameterType |
| 329 | + ?.FullName; |
| 330 | + }, |
| 331 | + context: out var context, |
| 332 | + logger: logger |
| 333 | + ); |
| 334 | + |
| 335 | + Assert.IsNotNull(result, nameof(result)); |
| 336 | + Assert.AreEqual(result, "Microsoft.Extensions.Logging.ILogger", nameof(result)); |
| 337 | + |
| 338 | + if (loadIntoReflectionOnlyContext) { |
| 339 | + Assert.IsNull(context, nameof(context)); |
| 340 | + return; |
| 341 | + } |
| 342 | + |
| 343 | + Assert.IsNotNull(context, nameof(context)); |
| 344 | + |
| 345 | + var unloaded = false; |
| 346 | + |
| 347 | + for (var i = 0; i < 10; i++) { |
| 348 | + if (!context!.IsAlive) { |
| 349 | + unloaded = true; |
| 350 | + break; |
| 351 | + } |
| 352 | + |
| 353 | + GC.Collect(); |
| 354 | + GC.WaitForPendingFinalizers(); |
| 355 | + } |
| 356 | + |
289 | 357 | Assert.IsTrue(unloaded, nameof(unloaded)); |
290 | 358 | } |
291 | 359 | } |
0 commit comments