Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapster.Tool catch ReflectionTypeLoadException #544

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Mapster.Core/Utils/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace Mapster.Utils
{
static class Extensions
public static class Extensions
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made this public to use from Mapster and Mapster.Tool. Maybe not ideal if it gets published?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me to be honest, nothing problematic being exposed in that class.

{
#if NET40
public static Type GetTypeInfo(this Type type)
Expand All @@ -27,5 +30,16 @@ public static string GetMemberName(this LambdaExpression lambda)
return prop;
}

public static IEnumerable<Type> GetLoadableTypes(this Assembly assembly)
{
try
{
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException e)
{
return e.Types.Where(t => t != null).Cast<Type>();
}
}
}
}
114 changes: 0 additions & 114 deletions src/Mapster.Tool/AssemblyResolver.cs

This file was deleted.

5 changes: 3 additions & 2 deletions src/Mapster.Tool/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Mapster.Utils;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -174,7 +175,7 @@ public static Type MakeNullable(this Type type)

public static void Scan(this CodeGenerationConfig config, Assembly assembly)
{
var registers = assembly.GetTypes()
var registers = assembly.GetLoadableTypes()
.Where(x => typeof(ICodeGenerationRegister).GetTypeInfo().IsAssignableFrom(x.GetTypeInfo()) &&
x.GetTypeInfo().IsClass && !x.GetTypeInfo().IsAbstract)
.Select(type => (ICodeGenerationRegister) Activator.CreateInstance(type)!);
Expand Down
16 changes: 7 additions & 9 deletions src/Mapster.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using CommandLine;
using ExpressionDebugger;
using Mapster.Models;
using Mapster.Utils;

namespace Mapster.Tool
{
Expand Down Expand Up @@ -58,13 +59,12 @@ private static void WriteFile(string code, string path)

private static void GenerateMappers(MapperOptions opt)
{
using var dynamicContext = new AssemblyResolver(Path.GetFullPath(opt.Assembly));
var assembly = dynamicContext.Assembly;
var assembly = Assembly.LoadFrom(Path.GetFullPath(opt.Assembly));
var config = TypeAdapterConfig.GlobalSettings;
config.SelfContainedCodeGeneration = true;
config.Scan(assembly);

foreach (var type in assembly.GetTypes())
foreach (var type in assembly.GetLoadableTypes())
{
if (!type.IsInterface)
continue;
Expand Down Expand Up @@ -149,12 +149,11 @@ private static string GetImplName(string name)

private static void GenerateModels(ModelOptions opt)
{
using var dynamicContext = new AssemblyResolver(Path.GetFullPath(opt.Assembly));
var assembly = dynamicContext.Assembly;
var assembly = Assembly.LoadFrom(Path.GetFullPath(opt.Assembly));
var codeGenConfig = new CodeGenerationConfig();
codeGenConfig.Scan(assembly);

var types = assembly.GetTypes().ToHashSet();
var types = assembly.GetLoadableTypes().ToHashSet();
foreach (var builder in codeGenConfig.AdaptAttributeBuilders)
{
foreach (var setting in builder.TypeSettings)
Expand Down Expand Up @@ -381,8 +380,7 @@ private static void ApplySettings(TypeAdapterSetter setter, BaseAdaptAttribute a

private static void GenerateExtensions(ExtensionOptions opt)
{
using var dynamicContext = new AssemblyResolver(Path.GetFullPath(opt.Assembly));
var assembly = dynamicContext.Assembly;
var assembly = Assembly.LoadFrom(Path.GetFullPath(opt.Assembly));
var config = TypeAdapterConfig.GlobalSettings;
config.SelfContainedCodeGeneration = true;
config.Scan(assembly);
Expand All @@ -397,7 +395,7 @@ private static void GenerateExtensions(ExtensionOptions opt)
assemblies.Add(setting.Key.Assembly);
}
}
var types = assemblies.SelectMany(it => it.GetTypes()).ToHashSet();
var types = assemblies.SelectMany(it => it.GetLoadableTypes()).ToHashSet();
var configDict = new Dictionary<BaseAdaptAttribute, TypeAdapterConfig>();
foreach (var builder in codeGenConfig.AdaptAttributeBuilders)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Mapster/TypeAdapterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ public void CompileProjection(Type sourceType, Type destinationType)

public IList<IRegister> Scan(params Assembly[] assemblies)
{
List<IRegister> registers = assemblies.Select(assembly => assembly.GetTypes()
List<IRegister> registers = assemblies.Select(assembly => assembly.GetLoadableTypes()
.Where(x => typeof(IRegister).GetTypeInfo().IsAssignableFrom(x.GetTypeInfo()) && x.GetTypeInfo().IsClass && !x.GetTypeInfo().IsAbstract))
.SelectMany(registerTypes =>
registerTypes.Select(registerType => (IRegister)Activator.CreateInstance(registerType))).ToList();
Expand Down