我有以下插件:運行代碼
IPlugin.cs:
namespace StandalonePlugin
{
public interface IPlugin
{
string Execute(string data);
}
}
StandalonePlugin.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();
}
}
}
最後的代碼加載插件,並呼籲它的執行方法:
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。我想知道爲什麼沒有顯示AppDomain PluginDomain
。看起來像使用父AppDomain,即使我想爲插件創建額外的AppDomain
http://msdn.microsoft.com/en-us/library/ms242202.aspx –
@拉斐爾奧爾索斯:我禁用了Visual Studio的託管過程中,仍然獲取LearningPlugins.exe作爲AppDomain。我的問題是:爲什麼不是我使用的專門創建的PluginDomain? – lowerkey