Skip to content

Commit

Permalink
Match ReflectionTypeLoadException handling from LibraryWriter int…
Browse files Browse the repository at this point in the history
…o `ComAliasNameResolver` (#205)

* Add handling for ReflectionTypeLoadException to ComAliasNameWriter

* Add sample library to reproduce export despite missing references
  • Loading branch information
mgaffigan committed Nov 15, 2023
1 parent 143952a commit 45dd52d
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 19 deletions.
11 changes: 11 additions & 0 deletions dscom.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dscom.build", "src\dscom.bu
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "assembly4", "src\dscom.demo\assembly4\assembly4.csproj", "{375866D7-1313-408D-AE5A-A77E48711EDB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "assembly5", "src\dscom.demo\assembly5\assembly5.csproj", "{ACECABE4-AD32-4618-889F-E210D9564C8F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -95,6 +97,14 @@ Global
{375866D7-1313-408D-AE5A-A77E48711EDB}.Release|Any CPU.Build.0 = Release|Any CPU
{375866D7-1313-408D-AE5A-A77E48711EDB}.Release|x86.ActiveCfg = Release|x86
{375866D7-1313-408D-AE5A-A77E48711EDB}.Release|x86.Build.0 = Release|x86
{ACECABE4-AD32-4618-889F-E210D9564C8F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ACECABE4-AD32-4618-889F-E210D9564C8F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ACECABE4-AD32-4618-889F-E210D9564C8F}.Debug|x86.ActiveCfg = Debug|x86
{ACECABE4-AD32-4618-889F-E210D9564C8F}.Debug|x86.Build.0 = Debug|x86
{ACECABE4-AD32-4618-889F-E210D9564C8F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ACECABE4-AD32-4618-889F-E210D9564C8F}.Release|Any CPU.Build.0 = Release|Any CPU
{ACECABE4-AD32-4618-889F-E210D9564C8F}.Release|x86.ActiveCfg = Release|x86
{ACECABE4-AD32-4618-889F-E210D9564C8F}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -109,6 +119,7 @@ Global
{5B402A1B-18B1-4D88-804A-BC0E58EF3730} = {0A2E33B4-9DF7-4199-BC39-B0FC9C99FA97}
{F8F68E57-CFFE-4EA5-9C1A-2CD9223B5D85} = {0A2E33B4-9DF7-4199-BC39-B0FC9C99FA97}
{375866D7-1313-408D-AE5A-A77E48711EDB} = {CAAB6257-7EC0-484E-9593-B60CEE8F47D1}
{ACECABE4-AD32-4618-889F-E210D9564C8F} = {CAAB6257-7EC0-484E-9593-B60CEE8F47D1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3F23D9EA-A9D9-4CE5-94B5-050A9B68CA1C}
Expand Down
7 changes: 7 additions & 0 deletions src/dscom.demo/assembly5/ExplodeyBaseClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System.Windows.Controls;

namespace dSPACE.Runtime.InteropServices.DemoAssembly5;

public class ExplodeyBaseClass : UserControl
{
}
9 changes: 9 additions & 0 deletions src/dscom.demo/assembly5/ExplodeyMember.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Windows.Controls;

namespace dSPACE.Runtime.InteropServices.DemoAssembly5;

public class ExplodeyMember
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Example")]
public UserControl Control => null!;
}
13 changes: 13 additions & 0 deletions src/dscom.demo/assembly5/ExplodeyParameter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Windows.Controls;

namespace dSPACE.Runtime.InteropServices.DemoAssembly5;

public class ExplodeyParameter
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "example")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "example")]
public void Example(UserControl thing)
{
// nop
}
}
9 changes: 9 additions & 0 deletions src/dscom.demo/assembly5/IExportableType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Runtime.InteropServices;

namespace dSPACE.Runtime.InteropServices.DemoAssembly5;

[ComVisible(true), Guid("E27C3BDB-ACEF-44F9-8568-481D44681C04"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IExportableType
{
void DoIt();
}
3 changes: 3 additions & 0 deletions src/dscom.demo/assembly5/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.InteropServices;

[assembly: ComVisible(false)]
25 changes: 25 additions & 0 deletions src/dscom.demo/assembly5/assembly5.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0-windows;net48</TargetFrameworks>
<PlatformTarget>AnyCPU</PlatformTarget>
<LangVersion>10.0</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>dSPACE.Runtime.InteropServices.DemoAssembly5</RootNamespace>
<AssemblyName>dSPACE.Runtime.InteropServices.DemoAssembly5</AssemblyName>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<AnalysisMode>Recommended</AnalysisMode>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<Platforms>AnyCPU;x86</Platforms>
<EnableComHosting>true</EnableComHosting>
</PropertyGroup>
<PropertyGroup>
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\dscom\dscom.csproj" />
</ItemGroup>

</Project>
20 changes: 20 additions & 0 deletions src/dscom/AssemblyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,24 @@ public static TypeLibIdentifier GetLibIdentifier(this Assembly assembly)
{
return assembly.GetLibIdentifier(Guid.Empty);
}

internal static IEnumerable<Type> GetLoadableTypes(this Assembly assembly)
{
return GetLoadableTypesAndLog(assembly, null);
}

internal static IEnumerable<Type> GetLoadableTypesAndLog(this Assembly assembly, WriterContext? context)
{
try
{
return assembly.GetTypes();
}
// https://stackoverflow.com/questions/7889228/how-to-prevent-reflectiontypeloadexception-when-calling-assembly-gettypes
catch (ReflectionTypeLoadException e)
{
context?.LogWarning($"Type library exporter encountered an error while processing '{assembly.GetName().Name}'. Error: {e.LoaderExceptions.First()!.Message}");

return e.Types.Where(t => t is not null)!;
}
}
}
43 changes: 43 additions & 0 deletions src/dscom/MethodBaseExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2022 dSPACE GmbH, Mark Lechtermann, Matthias Nissen and Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.ComponentModel;
using System.Reflection;

namespace dSPACE.Runtime.InteropServices;

/// <summary>
/// Extension methods for <see cref="MethodBase"/>.
/// </summary>
[Browsable(false)]
internal static class MethodBaseExtensions
{
/// <summary>
/// Return the parameters defined in a method, or an empty array if the
/// parameter types are unloadable
/// </summary>
/// <param name="method">The method to enumerate</param>
/// <returns>The loadable parameters</returns>
internal static IEnumerable<ParameterInfo> GetLoadableParameters(this MethodInfo method)
{
try
{
return method.GetParameters();
}
catch
{
return Array.Empty<ParameterInfo>();
}
}
}
61 changes: 61 additions & 0 deletions src/dscom/TypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2022 dSPACE GmbH, Mark Lechtermann, Matthias Nissen and Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.ComponentModel;
using System.Reflection;

namespace dSPACE.Runtime.InteropServices;

/// <summary>
/// Extension methods for <see cref="Type"/>.
/// </summary>
[Browsable(false)]
internal static class TypeExtensions
{
/// <summary>
/// Return the members defined in a type, or an empty array if the
/// members cannot be enumerated.
/// </summary>
/// <param name="type">The type to enumerate</param>
/// <returns>The members which were able to be enumerated</returns>
internal static IEnumerable<MemberInfo> GetLoadableMembers(this Type type)
{
try
{
return type.GetMembers();
}
catch
{
return Array.Empty<MemberInfo>();
}
}

/// <summary>
/// Return the methods defined in a type, or an empty array if the
/// methods cannot be enumerated.
/// </summary>
/// <param name="type">The type to enumerate</param>
/// <returns>The Methods which were able to be enumerated</returns>
internal static IEnumerable<MethodInfo> GetLoadableMethods(this Type type)
{
try
{
return type.GetMethods();
}
catch
{
return Array.Empty<MethodInfo>();
}
}
}
8 changes: 4 additions & 4 deletions src/dscom/names/ComAliasNameResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal sealed class ComAliasNameResolver : INameResolver

public ComAliasNameResolver(Assembly assembly)
{
var comVisibleTypes = assembly.GetTypes().Where(t => t.IsPublic && t.GetCustomAttribute<ComVisibleAttribute>() != null);
var comVisibleTypes = assembly.GetLoadableTypes().Where(t => t.IsPublic && t.GetCustomAttribute<ComVisibleAttribute>() != null);
var types = comVisibleTypes
.Where(t => t.GetCustomAttribute<ComAliasAttribute>() != null)
.ToDictionary(t => t as object, t => t.GetCustomAttribute<ComAliasAttribute>()?.Alias ?? string.Empty)
Expand All @@ -35,7 +35,7 @@ public ComAliasNameResolver(Assembly assembly)
}

var members = comVisibleTypes
.SelectMany(t => t.GetMembers().Where(m => m.GetCustomAttribute<ComAliasAttribute>() != null))
.SelectMany(t => t.GetLoadableMembers().Where(m => m.GetCustomAttribute<ComAliasAttribute>() != null))
.ToDictionary(m => m as object, m => m.GetCustomAttribute<ComAliasAttribute>()?.Alias ?? string.Empty)
;
foreach (var kv in members)
Expand All @@ -44,8 +44,8 @@ public ComAliasNameResolver(Assembly assembly)
}

var parameters = comVisibleTypes
.SelectMany(t => t.GetMethods())
.SelectMany(m => m.GetParameters().Where(p => p.GetCustomAttribute<ComAliasAttribute>() != null))
.SelectMany(t => t.GetLoadableMethods())
.SelectMany(m => m.GetLoadableParameters().Where(p => p.GetCustomAttribute<ComAliasAttribute>() != null))
.ToDictionary(p => p as object, p => p.GetCustomAttribute<ComAliasAttribute>()?.Alias ?? string.Empty)
;
foreach (var kv in parameters)
Expand Down
16 changes: 1 addition & 15 deletions src/dscom/writer/LibraryWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,7 @@ private void CollectAllTypes()
var comVisibleAttributeAssembly = Assembly.GetCustomAttribute<ComVisibleAttribute>();
var typesAreVisibleForComByDefault = comVisibleAttributeAssembly == null || comVisibleAttributeAssembly.Value;

Type?[] types;
try
{
types = Assembly.GetTypes().ToArray();
}
catch (ReflectionTypeLoadException e)
{
Context.LogWarning($"Type library exporter encountered an error while processing '{Assembly.GetName().Name}'. Error: {e.LoaderExceptions.First()!.Message}");

if (!e.Types.Any(t => t != null))
{
throw e.LoaderExceptions.First()!;
}
types = e.Types;
}
var types = Assembly.GetLoadableTypesAndLog(Context);

List<TypeWriter> classInterfaceWriters = new();

Expand Down

0 comments on commit 45dd52d

Please sign in to comment.