从自定义应用程序域运行代码

本文关键字:代码 运行 应用程序域 自定义 | 更新日期: 2023-09-27 18:35:01

我有以下插件:

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。似乎使用了父应用程序域,即使我想为插件创建一个额外的应用程序域

从自定义应用程序域运行代码

在调试模式下启动应用程序时,vshost 将添加到友好名称中。解决方法是使用正则表达式

string myName = Regex.Replace(AppDomain.CurrentDomain.FriendlyName, 
    @"(?<basename>.*).vshost(?<extension>'..*)", "${basename}${extension}");

vshost
的目的您也可以检查AppDomain.FriendlyName问题

这是因为您使用的是AppDomain.CurrentDomain.FriendlyName .

如 msdn ( http://msdn.microsoft.com/en-us/library/system.appdomain.friendlyname 中所述(

默认应用程序域的友好名称是文件名 的进程可执行文件

=> 学习插件.exe

为您

所以你应该做这样的东西

在主:

Console.WriteLine(plugin.Execute("Test", pluginDomain));

在独立插件中:

public string Execute(string data, AppDomain childDomain)
        {
            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", childDomain.FriendlyName);//CHANGE HERE
            result.AppendFormat("Current Dir: {0}'r'n", Environment.CurrentDirectory);
            result.AppendFormat("Assembly location: {0}'r'n", Assembly.GetExecutingAssembly().Location);
            return result.ToString();
        }