Skip to content

Commit a4da3f1

Browse files
authored
Add missing usings (#20481)
The example was incomplete; `System.Threading.Thread` was being referenced without the namespace being properly imported.
1 parent bd1088e commit a4da3f1

File tree

4 files changed

+78
-58
lines changed

4 files changed

+78
-58
lines changed
Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
class TestAssemblyLoadContext : AssemblyLoadContext
2-
{
3-
private AssemblyDependencyResolver _resolver;
1+
using System.Reflection;
2+
using System.Runtime.Loader;
43

5-
public TestAssemblyLoadContext(string mainAssemblyToLoadPath) : base(isCollectible: true)
4+
namespace complex
5+
{
6+
class TestAssemblyLoadContext : AssemblyLoadContext
67
{
7-
_resolver = new AssemblyDependencyResolver(mainAssemblyToLoadPath);
8-
}
8+
private AssemblyDependencyResolver _resolver;
99

10-
protected override Assembly Load(AssemblyName name)
11-
{
12-
string assemblyPath = _resolver.ResolveAssemblyToPath(name);
13-
if (assemblyPath != null)
10+
public TestAssemblyLoadContext(string mainAssemblyToLoadPath) : base(isCollectible: true)
1411
{
15-
return LoadFromAssemblyPath(assemblyPath);
12+
_resolver = new AssemblyDependencyResolver(mainAssemblyToLoadPath);
1613
}
1714

18-
return null;
15+
protected override Assembly Load(AssemblyName name)
16+
{
17+
string assemblyPath = _resolver.ResolveAssemblyToPath(name);
18+
if (assemblyPath != null)
19+
{
20+
return LoadFromAssemblyPath(assemblyPath);
21+
}
22+
23+
return null;
24+
}
1925
}
2026
}
Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,66 @@
1-
using System.Reflection;
1+
using System;
2+
using System.Reflection;
23
using System.Runtime.CompilerServices;
34
using System.Runtime.Loader;
45

5-
// <Snippet1>
6-
class TestAssemblyLoadContext : AssemblyLoadContext
6+
namespace simple
77
{
8-
public TestAssemblyLoadContext() : base(isCollectible: true)
8+
// <Snippet1>
9+
class TestAssemblyLoadContext : AssemblyLoadContext
910
{
10-
}
11+
public TestAssemblyLoadContext() : base(isCollectible: true)
12+
{
13+
}
1114

12-
protected override Assembly Load(AssemblyName name)
13-
{
14-
return null;
15+
protected override Assembly Load(AssemblyName name)
16+
{
17+
return null;
18+
}
1519
}
16-
}
17-
// </Snippet1>
20+
// </Snippet1>
1821

19-
class Test
20-
{
21-
// <Snippet2>
22-
[MethodImpl(MethodImplOptions.NoInlining)]
23-
static int ExecuteAndUnload(string assemblyPath, out WeakReference alcWeakRef)
22+
class Test
2423
{
25-
// <Snippet3>
26-
var alc = new TestAssemblyLoadContext();
27-
Assembly a = alc.LoadFromAssemblyPath(assemblyPath);
28-
// </Snippet3>
29-
30-
alcWeakRef = new WeakReference(alc, trackResurrection: true);
24+
// <Snippet2>
25+
[MethodImpl(MethodImplOptions.NoInlining)]
26+
static int ExecuteAndUnload(string assemblyPath, out WeakReference alcWeakRef)
27+
{
28+
// <Snippet3>
29+
var alc = new TestAssemblyLoadContext();
30+
Assembly a = alc.LoadFromAssemblyPath(assemblyPath);
31+
// </Snippet3>
3132

32-
// <Snippet4>
33-
var args = new object[1] { new string[] {"Hello"}};
34-
int result = (int)a.EntryPoint.Invoke(null, args);
35-
// </Snippet4>
33+
alcWeakRef = new WeakReference(alc, trackResurrection: true);
3634

37-
// <Snippet5>
38-
alc.Unload();
39-
// </Snippet5>
35+
// <Snippet4>
36+
var args = new object[1] {new string[] {"Hello"}};
37+
int result = (int) a.EntryPoint.Invoke(null, args);
38+
// </Snippet4>
4039

41-
return result;
42-
}
43-
// </Snippet2>
40+
// <Snippet5>
41+
alc.Unload();
42+
// </Snippet5>
4443

45-
static void ExecuteAssemblyInUnloadableContext()
46-
{
47-
// <Snippet6>
48-
WeakReference testAlcWeakRef;
49-
int result = ExecuteAndUnload("absolute/path/to/your/assembly", out testAlcWeakRef);
50-
// </Snippet6>
44+
return result;
45+
}
46+
// </Snippet2>
5147

52-
// <Snippet7>
53-
for (int i = 0; testAlcWeakRef.IsAlive && (i < 10); i++)
48+
static void ExecuteAssemblyInUnloadableContext()
5449
{
55-
GC.Collect();
56-
GC.WaitForPendingFinalizers();
57-
}
58-
// </Snippet7>
50+
// <Snippet6>
51+
WeakReference testAlcWeakRef;
52+
int result = ExecuteAndUnload("absolute/path/to/your/assembly", out testAlcWeakRef);
53+
// </Snippet6>
54+
55+
// <Snippet7>
56+
for (int i = 0; testAlcWeakRef.IsAlive && (i < 10); i++)
57+
{
58+
GC.Collect();
59+
GC.WaitForPendingFinalizers();
60+
}
61+
// </Snippet7>
5962

60-
bool success = testAlcWeakRef.IsAlive;
63+
bool success = testAlcWeakRef.IsAlive;
64+
}
6165
}
62-
}
66+
}

samples/snippets/standard/assembly/unloading/unloadability_issues_example_test.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Runtime.InteropServices;
3+
using System.Threading;
34

45
namespace test
56
{
@@ -12,7 +13,7 @@ class Program
1213
{
1314
public static void ThreadProc()
1415
{
15-
// Issue preventing unlopading #4 - a thread running method inside of the TestAssemblyLoadContext at the unload time
16+
// Issue preventing unloading #4 - a thread running method inside of the TestAssemblyLoadContext at the unload time
1617
Thread.Sleep(Timeout.Infinite);
1718
}
1819

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
<StartupObject>test.Program</StartupObject>
7+
</PropertyGroup>
8+
9+
</Project>

0 commit comments

Comments
 (0)