Skip to content

Commit

Permalink
添加启动时加载插件
Browse files Browse the repository at this point in the history
  • Loading branch information
uyoufu committed May 14, 2022
1 parent 056fc37 commit 1a5e32a
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 27 deletions.
27 changes: 22 additions & 5 deletions MSAddinTest/Core/Command/RunPluginCommand.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using MSAddinTest.MSTestInterface;
using Bentley.MstnPlatformNET;
using MSAddinTest.MSTestInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
Expand All @@ -27,16 +30,30 @@ public RunPluginCommand(string excutorName)
_args = new MSTestArg();
}

/// <summary>
/// 特性控制可以跨程序集捕获异常
/// </summary>
/// <returns></returns>
//[HandleProcessCorruptedStateExceptions,SecurityCritical]
public override FuncResult Start()
{
int executorsCount = 0;
foreach (var kv in PluginContainer)

// 全部包裹在 TryCatch中
try
{
foreach (var kv in PluginContainer)
{
var result = kv.Value.Execute(_executorName, _args);
if (result.Data is int count) executorsCount += count;
}
}
catch (Exception ex)
{
var result = kv.Value.Execute(_executorName, _args);
if (result.Data is int count) executorsCount += count;
MessageCenter.Instance.ShowErrorMessage(ex.Message, ex.StackTrace, true);
}

if(executorsCount == 0)
if (executorsCount == 0)
{
MessageBox.Show($"未找到名为{_executorName}的执行器");
}
Expand Down
2 changes: 1 addition & 1 deletion MSAddinTest/Core/Executor/AddinExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public override void Execute(IMSTestArg pluginArg)
{
string unparsedParams = string.Empty;
if (pluginArg is MSTestArg arg) unparsedParams = arg.UnparsedParams;
MethodInfo.Invoke(null, new object[] { unparsedParams });

MethodInfo.Invoke(null, new object[] { unparsedParams });
}
catch (Exception ex)
{
Expand Down
1 change: 0 additions & 1 deletion MSAddinTest/Core/Loader/PluginAssemblyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public FuncResult LoadAssembly()
else
_lastFileHash = newFileHash;


// 读取文件然后加载
byte[] bytes = File.ReadAllBytes(Setup.DllFullPath);
var assembly = Assembly.Load(bytes);
Expand Down
9 changes: 8 additions & 1 deletion MSAddinTest/Core/Loader/PluginAssemblyLoader_Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MSAddinTest.Core.Loader
{
Expand All @@ -19,12 +20,18 @@ private void RegistryEvents()
appDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
appDomain.TypeResolve += AppDomain_TypeResolve;
appDomain.UnhandledException += CurrentDomain_UnhandledException;
appDomain.FirstChanceException += AppDomain_FirstChanceException;
}

private void AppDomain_FirstChanceException(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
{
//MessageBox.Show(e.Exception.Message);
}

#region 加载需要的程序集
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
;
MessageBox.Show(e.ExceptionObject.ToString());
}

// 某个类型未找到时,去加载类型
Expand Down
2 changes: 2 additions & 0 deletions MSAddinTest/Core/Loader/PluginAssemblyLoader_Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public FuncResult Execute(string name, IMSTestArg arg)
/// </summary>
public void Reload()
{
// 先对原来的 keyin 执行卸载程序

LoadAssembly();
}
}
Expand Down
5 changes: 5 additions & 0 deletions MSAddinTest/Index/MSAddin.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bentley.MstnPlatformNET;
using MSAddinTest.Core.Command;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -28,6 +29,10 @@ protected override int Run(string[] commandLine)

MessageCenter.Instance.ShowInfoMessage("MSAddintTest 加载成功!","",false);

// 加载自启动插件
var cmd = new LoadPluginsWhenStartupCommand();
Core.PluginManager.Manager.InvokeCommand(cmd);

return 0;
}
}
Expand Down
7 changes: 6 additions & 1 deletion MSAddinTest/MSTestInterface/MSTest_Addin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ protected MSTest_Addin(IntPtr mdlDescriptor) { }
/// 子类用于初始化
/// </summary>
/// <param name="addinArg"></param>
public abstract void Init(AddIn addIn);
public virtual void Init(AddIn addIn) { }

/// <summary>
/// 卸载后手动清除向 Addin 中添加的一些事件
/// </summary>
public virtual void Unloaded() { }
}
}
12 changes: 0 additions & 12 deletions MSUtils/Class1.cs

This file was deleted.

2 changes: 1 addition & 1 deletion MSUtils/MSUtils.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="TestClass.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
16 changes: 16 additions & 0 deletions MSUtils/TestClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MSUtils
{
public class TestClass
{
public static void NullException()
{
throw new NullReferenceException("test exception in reference");
}
}
}
8 changes: 6 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,15 @@ internal class PluginAddin : MSTest_Addin

}

// 重写初始化方法,获取 addin 参数
public override void Init(AddIn addin)
{
Instance = addin;
Run(new string[] { });
}

// 在这个方法中释放资源
public overrride void Unloaded() { }

protected override int Run(string[] commandLine)
{
Expand Down Expand Up @@ -260,7 +264,7 @@ dllName.autoLoad=true,autoReload=true

通过向默认域中加载不同版本的程序集来实现 DLL 版本的重载。为了可以重新编译已加载的 DLL,需要保证加载的 DLL 在被加载后不被锁定,本程序采用从内存的加载方式实现了这个需求。

该实现是一种伪热加载的实现方法,每次加载的 Dll 都会驻留在内存中,卸载的时候某个程序集时,只是丢弃了其引用,并没有从内存里释放。
该实现是一种伪热加载的实现方法,每次加载的 Dll 都会驻留在内存中,卸载某个程序集时,只是丢弃了其引用,并没有从内存里释放。

可能有人要问了,为什么采用这种方式呢?

Expand All @@ -278,7 +282,7 @@ dllName.autoLoad=true,autoReload=true

## UI

本插件预留了 UI 接口,但短期内不会实现。
本插件预留了 UI 接口,但短期内不会实现。欢迎同志们 PR

## 参考

Expand Down
6 changes: 6 additions & 0 deletions TestAddinPlugin/TestAddinPlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,11 @@
<LogicalName>CommandTable.xml</LogicalName>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MSUtils\MSUtils.csproj">
<Project>{E687EF99-6F1E-4255-A31F-F235257A9391}</Project>
<Name>MSUtils</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
8 changes: 5 additions & 3 deletions TestAddinPlugin/TestStaticMethodExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Bentley.GeometryNET;
using Bentley.MstnPlatformNET;
using MSAddinTest.MSTestInterface;
using MSUtils;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -47,10 +48,11 @@ public static object NewElement(IMSTestArg arg)
return true;
}

[MSTest("ref")]
[MSTest("exception")]
public static object TestReference(IMSTestArg arg)
{
return true;
{
TestClass.NullException();
return false;
}
}
}

0 comments on commit 1a5e32a

Please sign in to comment.