我有以下插件:
IPlugin.cs:
namespace StandalonePlugin
{
public interface IPlugin
{
string Execute(string data);
}
}
独立插件.cs:
namespace StandalonePlugin
{
[Export(typeof(IPlugin))]
public class StandalonePlugin : IPlugin
{
public string Execute(string data)
{
StringBuilder result = new StringBuilder();
result.AppendFormat("Plugintype: {0}\r\n", this.GetType().FullName);
result.AppendFormat("Data: {0}\r\n", data);
result.AppendFormat("AppDomain: {0}\r\n", AppDomain.CurrentDomain.FriendlyName);
result.AppendFormat("Current Dir: {0}\r\n", Environment.CurrentDirectory);
result.AppendFormat("Assembly location: {0}\r\n", Assembly.GetExecutingAssembly().Location);
return result.ToString();
}
}
}
最后是加载插件并调用它的 Execute 方法的代码:
namespace CustomAppDomain
{
class Program
{
static void Main(string[] args)
{
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
setup.PrivateBinPath = @".";
AppDomain pluginDomain = AppDomain.CreateDomain("PluginDomain", null, setup);
string pathToDll = @"D:\Code\LearningPlugins\StandalonePlugin\bin\Debug";
Type t = typeof(IPlugin);
IPlugin plugin = (IPlugin)pluginDomain.CreateInstanceFromAndUnwrap(pathToDll, t.FullName);
Console.WriteLine(plugin.Execute("Test"));
Console.WriteLine("<ENTER> to continue");
Console.ReadLine();
}
}
}
这是输出:
> Plugintype: StandalonePlugin.StandalonePlugin
> Data: Test
> AppDomain:LearningPlugins.vshost.exe
> Current Dir: D:\Code\LearningPlugins\LearningPlugins\bin\Debug
> Assembly location: D:\Code\LearningPlugins\LearningPlugins\bin\Debug\StandalonePlugin.dll
>
> <ENTER> to continue
现在的问题是:为什么 AppDomain 列出LearningPlugins.exe
而不是PluginDomain
, 中指定的AppDomain pluginDomain = AppDomain.CreateDomain("PluginDomain", null, setup);
?
澄清一下,我不在乎添加 vshost。我想知道为什么PluginDomain
不显示 AppDomain。即使我想为插件创建一个额外的 appdomain,似乎也使用了父 AppDomain