添加项目文件。
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
|
||||
namespace KGdev.BI3D.Revit.Addin.MainWindow.External.Browser
|
||||
{
|
||||
public class AssemblyLoaderUtil
|
||||
{
|
||||
public AssemblyLoaderUtil(string assemblyDir)
|
||||
{
|
||||
AssemblyDirectory = assemblyDir;
|
||||
}
|
||||
|
||||
public string AssemblyDirectory { get; set; }
|
||||
|
||||
public void HookAssemblyResolve()
|
||||
{
|
||||
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载目录下的所有程序集
|
||||
/// </summary>
|
||||
/// <param name="commandFolderPath"></param>
|
||||
/// <returns></returns>
|
||||
public List<Assembly> LoadAllRibbonAssemblies(string commandFolderPath)
|
||||
{
|
||||
var list = new List<Assembly>();
|
||||
foreach (var assemblyFile in Directory.GetFiles(commandFolderPath, "*.dll"))
|
||||
{
|
||||
try
|
||||
{
|
||||
list.Add(Assembly.LoadFrom(assemblyFile));
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
///// <summary>
|
||||
///// 调用此方法解决UI程序集丢失
|
||||
///// </summary>
|
||||
///// <param name="action"></param>
|
||||
//public static void ResolveAssembly(Action action)
|
||||
//{
|
||||
// string addInPath = typeof(AssemblyLoaderUtil).Assembly.Location;
|
||||
// string dirAssembly = Path.GetDirectoryName(addInPath);
|
||||
// AssemblyLoaderUtil loaderUtil = new AssemblyLoaderUtil(dirAssembly);
|
||||
// loaderUtil.HookAssemblyResolve();
|
||||
// try
|
||||
// {
|
||||
// action();
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// MessageBox.Show(ex.InnerException?.ToString());
|
||||
// }
|
||||
// finally
|
||||
// {
|
||||
// loaderUtil.UnhookAssemblyResolve();
|
||||
// }
|
||||
//}
|
||||
|
||||
public void UnhookAssemblyResolve()
|
||||
{
|
||||
AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
|
||||
}
|
||||
|
||||
private Assembly LoadAddin(string filePath)
|
||||
{
|
||||
Assembly result;
|
||||
try
|
||||
{
|
||||
Monitor.Enter(this);
|
||||
result = Assembly.LoadFile(filePath);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Monitor.Exit(this);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private string SearchAssemblyFile(string assemblyName)
|
||||
{
|
||||
string[] array = { ".dll", ".exe" };
|
||||
var str = assemblyName.Substring(0, assemblyName.IndexOf(','));
|
||||
foreach (var str2 in array)
|
||||
{
|
||||
var text = AssemblyDirectory + "\\" + str + str2;
|
||||
if (File.Exists(text))
|
||||
{
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
|
||||
{
|
||||
Assembly result;
|
||||
_ = new AssemblyName(args.Name);
|
||||
var text = SearchAssemblyFile(args.Name);
|
||||
if (File.Exists(text))
|
||||
{
|
||||
result = LoadAddin(text);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (string.IsNullOrEmpty(text))
|
||||
{
|
||||
var array = args.Name.Split(',');
|
||||
var text2 = array[0];
|
||||
if (array.Length > 1)
|
||||
{
|
||||
var text3 = array[2];
|
||||
if (
|
||||
text2.EndsWith(".resources", StringComparison.CurrentCultureIgnoreCase)
|
||||
&& !text3.EndsWith("neutral", StringComparison.CurrentCultureIgnoreCase)
|
||||
)
|
||||
{
|
||||
text2 = text2.Substring(0, text2.Length - ".resources".Length);
|
||||
}
|
||||
|
||||
text = SearchAssemblyFile(text2);
|
||||
if (File.Exists(text))
|
||||
{
|
||||
return LoadAddin(text);
|
||||
}
|
||||
//text = this.SearchAssemblyFile(text2);
|
||||
}
|
||||
}
|
||||
|
||||
result = LoadAddin(text);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user