Skip to content

Commit c261033

Browse files
committed
throw an exception or log the situation when forwarded types could not be loaded
1 parent 8336146 commit c261033

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
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+
#pragma warning disable CA1848
4+
35
using System;
46
using System.Collections.Generic;
57
using System.IO;
@@ -196,6 +198,25 @@ public void WriteExportedTypes()
196198
// in the case of reference assembly cannot be loaded
197199
throw AssemblyFileNotFoundException.Create(assembly.GetName(), ex);
198200
}
201+
catch (ReflectionTypeLoadException ex) {
202+
// in the case of the forwarded type could not load or not found in assembly
203+
if (options.Writer.ThrowIfForwardedTypesCouldNotLoaded)
204+
throw;
205+
206+
foreach (var exLoader in ex.LoaderExceptions) {
207+
if (exLoader is not null && !string.IsNullOrEmpty(exLoader.Message))
208+
logger?.LogWarning("LoaderException: {ExceptionMessage}", exLoader.Message);
209+
}
210+
211+
logger?.LogWarning("ReflectionTypeLoadException: Could not load one or more forwarded types. If you are trying to load an assembly with an SDK version that is not currently installed, install that version of the SDK, or specify the 'DOTNET_ROLL_FORWARD' environment variable and try again.");
212+
213+
// append successfully loaded types
214+
if (ex.Types is not null) {
215+
types = types
216+
.Union(ex.Types.Where(static t => t is not null).Select(static t => t!))
217+
.ToList();
218+
}
219+
}
199220
#endif
200221

201222
var typeDeclarations = new StringBuilder(10240);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class ApiListWriterOptions : GeneratorOptions {
77

88
public class WriterOptions {
99
public bool OrderStaticMembersFirst { get; set; } = false;
10+
public bool ThrowIfForwardedTypesCouldNotLoaded { get; set; } = false;
1011
public bool WriteNullableAnnotationDirective { get; set; } = true;
1112

1213
/* options relevant to header */

0 commit comments

Comments
 (0)