Skip to content

Commit f9bb561

Browse files
authored
Merge pull request #64 from flysafely/dev
Fix dll dependency version conflicts
2 parents e3e0ecc + 3093c8b commit f9bb561

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

AddInManager/App.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,18 @@ public class App : IExternalApplication
1919
public static int ThemId { get; set; } = -1;
2020
public static DockablePaneId PaneId => new DockablePaneId(new Guid("942D8578-7F25-4DC3-8BD8-585C1DBD3614"));
2121
public static string PaneName => "Debug/Trace Output";
22+
23+
private static readonly Dictionary<string, string> KnownAssemblies = new Dictionary<string, string>(StringComparer.Ordinal)
24+
{
25+
{ "System.Resources.Extensions", "System.Resources.Extensions.dll" },
26+
{ "System.Runtime.CompilerServices.Unsafe", "System.Runtime.CompilerServices.Unsafe.dll" }
27+
};
28+
private static readonly ConcurrentDictionary<string, Assembly> AssemblyCache = new ConcurrentDictionary<string, Assembly>(StringComparer.Ordinal);
29+
private static readonly string BaseDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
2230

2331
public Result OnStartup(UIControlledApplication application)
2432
{
33+
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
2534
CreateRibbonPanel(application);
2635
application.ControlledApplication.DocumentClosed += DocumentClosed;
2736
DefaultSetting.Version += VersionChecker.CurrentVersion;
@@ -78,8 +87,35 @@ private static void AddPushButtonBipChecker(PulldownButton pullDownButton, Type
7887
buttonData.ToolTip = "Check Built-in Parameter of the Element";
7988
pullDownButton.AddPushButton(buttonData);
8089
}
90+
91+
private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
92+
{
93+
var requestedAssemblyName = new AssemblyName(args.Name).Name;
94+
if (AssemblyCache.TryGetValue(requestedAssemblyName, out var cachedAssembly))
95+
{
96+
return cachedAssembly;
97+
}
98+
if (KnownAssemblies.TryGetValue(requestedAssemblyName, out var assemblyFile))
99+
{
100+
var assemblyPath = Path.Combine(BaseDirectory, assemblyFile);
101+
102+
try
103+
{
104+
var assembly = Assembly.LoadFrom(assemblyPath);
105+
AssemblyCache.TryAdd(requestedAssemblyName, assembly); // 线程安全添加缓存
106+
return assembly;
107+
}
108+
catch (Exception ex)
109+
{
110+
Debug.WriteLine(ex.Message);
111+
}
112+
}
113+
114+
return null;
115+
}
116+
81117
private void DocumentClosed(object sender, Autodesk.Revit.DB.Events.DocumentClosedEventArgs e)
82118
{
83119
FrmAddInManager?.Close();
84120
}
85-
}
121+
}

0 commit comments

Comments
 (0)