-
-
Notifications
You must be signed in to change notification settings - Fork 254
Fix: Handle ReflectionTypeLoadException when loading assemblies #405
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,9 @@ | |
|
|
||
| #nullable enable | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using com.IvanMurzak.McpPlugin; | ||
| using com.IvanMurzak.ReflectorNet; | ||
| using com.IvanMurzak.Unity.MCP.Utils; | ||
|
|
@@ -95,12 +97,39 @@ protected virtual McpPlugin.Common.Version BuildVersion() | |
| return new UnityLoggerProvider(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Filters assemblies to only include those that can successfully load their types. | ||
| /// This prevents ReflectionTypeLoadException from assemblies with missing dependencies. | ||
| /// </summary> | ||
| protected virtual IEnumerable<Assembly> GetSafeAssemblies() | ||
| { | ||
| foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) | ||
| { | ||
| if (assembly.IsDynamic) | ||
| continue; | ||
|
|
||
| try | ||
| { | ||
| assembly.GetTypes(); | ||
| yield return assembly; | ||
| } | ||
| catch (ReflectionTypeLoadException) | ||
| { | ||
| _logger.LogWarning("Skipping assembly with missing dependencies: {assembly}", assembly.GetName().Name); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _logger.LogWarning("Skipping assembly {assembly}: {error}", assembly.GetName().Name, ex.Message); | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+104
to
+125
|
||
|
|
||
| protected virtual IMcpPlugin BuildMcpPlugin(McpPlugin.Common.Version version, Reflector reflector, ILoggerProvider? loggerProvider = null) | ||
| { | ||
| _logger.LogTrace("{method} called.", | ||
| nameof(BuildMcpPlugin)); | ||
|
|
||
| var assemblies = AppDomain.CurrentDomain.GetAssemblies(); | ||
| var assemblies = GetSafeAssemblies().ToArray(); | ||
| var mcpPlugin = new McpPluginBuilder(version, loggerProvider) | ||
| .WithConfig(config => | ||
| { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.