小编Yor*_*z0r的帖子

将程序集加载到applicationBase C#的AppDomain外部站点

最近我一直致力于一个项目,其中应用程序(或可执行文件,无论你想要什么)都需要能够加载和卸载在可执行文件夹找不到的程序集.(甚至可能是另一个驱动器)

为了举个例子,我希望能够将我的应用程序放在D:\ AAA\theAppFolder中,并将DLL文件的程序集放在C:\ BBB\Assemblies

仔细观察,我发现AppDomain允许卸载自己和任何附加程序集的能力,所以我想我会试一试,但是经过几个小时的尝试后似乎有问题:AppDomains看不到外面的任何地方应用基础.

根据AppDomain的纪录片(以及我自己的经验)你不能在ApplicationBase之外设置PrivateBinPath,如果我在应用程序所在的驱动器之外设置ApplicationBase(通过AppDomainSetup),我得到System.IO.FileNotFoundException抱怨它不能找到应用程序本身.

因此我甚至无法达到可以使用AssemblyResolve ResolveEventHandler尝试使用MarhsalByRefObject继承类来获取程序集的阶段...

这里有一些与我目前正在尝试的代码相关的代码片段

    internal class RemoteDomain : MarshalByRefObject
    {
        public override object InitializeLifetimeService() //there's apparently an error for marshalbyref objects where they get removed after a while without this
        {
            return null;
        }
        public Assembly GetAssembly(byte[] assembly)
        {
            try
            {
                return Assembly.Load(assembly);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return null;
        }
        public Assembly GetAssembly(string filepath)
        {
            try
            {
                return Assembly.LoadFrom(filepath);
            }
            catch …
Run Code Online (Sandbox Code Playgroud)

c# appdomain marshalbyrefobject .net-assembly

5
推荐指数
1
解决办法
6439
查看次数

标签 统计

.net-assembly ×1

appdomain ×1

c# ×1

marshalbyrefobject ×1