Skip to content

Commit eab24ec

Browse files
committed
make actionWithLoadedAssembly nullable
1 parent 253a8f0 commit eab24ec

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

src/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/AssemblyLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static TResult UsingAssembly<TArg, TResult>(
2727
FileInfo assemblyFile,
2828
bool loadIntoReflectionOnlyContext,
2929
TArg arg,
30-
Func<Assembly, TArg, TResult> actionWithLoadedAssembly,
30+
Func<Assembly, TArg, TResult>? actionWithLoadedAssembly,
3131
out WeakReference? context,
3232
ILogger? logger = null
3333
)

src/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/AssemblyLoader.netcore.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private static TResult UsingAssemblyCore<TArg, TResult>(
2727
AssemblySource assemblySource,
2828
bool loadIntoReflectionOnlyContext,
2929
TArg arg,
30-
Func<Assembly, TArg, TResult> actionWithLoadedAssembly,
30+
Func<Assembly, TArg, TResult>? actionWithLoadedAssembly,
3131
out WeakReference? context,
3232
ILogger? logger = null
3333
)
@@ -59,7 +59,7 @@ private static TResult UsingAssemblyCore<TArg, TResult>(
5959
private static TResult UsingReflectionOnlyAssembly<TArg, TResult>(
6060
AssemblySource assemblySource,
6161
TArg arg,
62-
Func<Assembly, TArg, TResult> actionWithLoadedAssembly,
62+
Func<Assembly, TArg, TResult>? actionWithLoadedAssembly,
6363
ILogger? logger = null
6464
)
6565
{
@@ -96,6 +96,9 @@ private static TResult UsingReflectionOnlyAssembly<TArg, TResult>(
9696
assemblyTypeFullName
9797
);
9898

99+
if (actionWithLoadedAssembly is null)
100+
return default;
101+
99102
return actionWithLoadedAssembly(assm, arg);
100103
}
101104

@@ -106,7 +109,7 @@ private static TResult UsingReflectionOnlyAssembly<TArg, TResult>(
106109
private static TResult UsingAssembly<TArg, TResult>(
107110
AssemblySource assemblySource,
108111
TArg arg,
109-
Func<Assembly, TArg, TResult> actionWithLoadedAssembly,
112+
Func<Assembly, TArg, TResult>? actionWithLoadedAssembly,
110113
out WeakReference? context,
111114
ILogger? logger = null
112115
)
@@ -148,6 +151,9 @@ private static TResult UsingAssembly<TArg, TResult>(
148151
);
149152

150153
try {
154+
if (actionWithLoadedAssembly is null)
155+
return default;
156+
151157
return actionWithLoadedAssembly(assm, arg);
152158
}
153159
finally {

tests/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/AssemblyLoader.cs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// SPDX-FileCopyrightText: 2021 smdn <smdn@smdn.jp>
22
// SPDX-License-Identifier: MIT
3+
#nullable enable
4+
35
using System;
46
using System.Linq;
57
using System.IO;
@@ -22,7 +24,7 @@ namespace Smdn.Reflection.ReverseGenerating.ListApi;
2224

2325
[TestFixture]
2426
class AssemblyLoaderTests {
25-
private ILogger logger = null;
27+
private ILogger? logger = null;
2628

2729
[OneTimeSetUp]
2830
public void Init()
@@ -152,6 +154,54 @@ public void UsingAssembly(bool loadIntoReflectionOnlyContext, string targetFrame
152154
Assert.IsTrue(unloaded, nameof(unloaded));
153155
}
154156

157+
#if NETCOREAPP3_1_OR_GREATER || NET6_0_OR_GREATER
158+
[TestCase(true, "netstandard2.1")]
159+
[TestCase(false, "netstandard2.1")]
160+
#endif
161+
public void UsingAssembly_ArgumentNull_ActionWithLoadedAssembly(bool loadIntoReflectionOnlyContext, string targetFrameworkMoniker)
162+
{
163+
var assemblyFile = new FileInfo(
164+
TestAssemblyInfo.TestAssemblyPaths.First(f => f.Contains(targetFrameworkMoniker) && f.Contains("LibA.dll"))
165+
);
166+
167+
WeakReference? context = null;
168+
int result = 1;
169+
170+
Assert.DoesNotThrow(() => {
171+
result = AssemblyLoader.UsingAssembly<int, int>(
172+
assemblyFile,
173+
loadIntoReflectionOnlyContext: loadIntoReflectionOnlyContext,
174+
arg: int.MaxValue,
175+
actionWithLoadedAssembly: null,
176+
context: out context,
177+
logger: logger
178+
);
179+
});
180+
181+
Assert.AreEqual(default(int), result, nameof(result));
182+
183+
if (loadIntoReflectionOnlyContext) {
184+
Assert.IsNull(context, nameof(context));
185+
return;
186+
}
187+
188+
Assert.IsNotNull(context, nameof(context));
189+
190+
var unloaded = false;
191+
192+
for (var i = 0; i < 10; i++) {
193+
if (!context!.IsAlive) {
194+
unloaded = true;
195+
break;
196+
}
197+
198+
GC.Collect();
199+
GC.WaitForPendingFinalizers();
200+
}
201+
202+
Assert.IsTrue(unloaded, nameof(unloaded));
203+
}
204+
155205
#if NETCOREAPP3_1_OR_GREATER || NET6_0_OR_GREATER
156206
[TestCase(true, "netstandard2.1")]
157207
[TestCase(false, "netstandard2.1")]

0 commit comments

Comments
 (0)