0

我在运行时编译程序集并通过添加到新域来链接它。我使用它,然后卸载域。但是,当我在同一次运行期间再次尝试编译时,我无法访问该程序集,因为它当前正在使用中。

这是我的一些代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary2
{
    public interface IExtension
    {
        String GetExtensionName();
    }
}

我的集会

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary2;

namespace ClassLibrary1
{
    public class Extension1 : MarshalByRefObject, IExtension
    {
        public Extension1()
        {
        }

        public string GetExtensionName()
        {
            return "Extension 1 from " + AppDomain.CurrentDomain.FriendlyName;
        }
    }
}

以及使用它的应用程序

namespace ConsoleApplication7
{
    class Program
    {
        static IEnumerable<IExtension> extensions;
        static void Main(string[] args)
        {
            // Create app domain
            AppDomain domain = CreateDomain(Directory.GetCurrentDirectory());
            try
            {
                // Get extensions
                extensions = EnumerateExtensions(domain);
                foreach (IExtension extension in extensions)
                    // Execute extension method in separate domain.
                    Console.WriteLine(extension.GetExtensionName());

                // Unload domain

                UnloadDomain(domain);
            }
            finally
            {
                domain = null;
                GC.Collect(2);
                extensions = null;
            }

            Console.ReadKey();
        }            

        private static IEnumerable<IExtension> EnumerateExtensions(AppDomain domain)
        {
            IEnumerable<string> fileNames = Directory.EnumerateFiles(domain.BaseDirectory, "*.dll");
            if (fileNames != null)
            {
                foreach (string assemblyFileName in fileNames)
                {
                    foreach (string typeName in GetTypes(assemblyFileName, typeof(IExtension), domain))
                    {
                        System.Runtime.Remoting.ObjectHandle handle;
                        try
                        {
                            handle = domain.CreateInstanceFrom(assemblyFileName, typeName);
                        }
                        catch (MissingMethodException)
                        {
                            continue;
                        }
                        object obj = handle.Unwrap();
                        IExtension extension = (IExtension)obj;
                        yield return extension;
                    }
                }
            }

        }    

        private static IEnumerable<string> GetTypes(string assemblyFileName, Type interfaceFilter, AppDomain domain)
        {
            Assembly asm = domain.Load(AssemblyName.GetAssemblyName(assemblyFileName));
            Type[] types = asm.GetTypes();
            foreach (Type type in types)
            {
                if (type.GetInterface(interfaceFilter.Name) != null)
                {
                    yield return type.FullName;
                }
            }
        }    

        static AppDomain CreateDomain(string path)
        {
            AppDomainSetup setup = new AppDomainSetup();
            setup.ApplicationBase = path;
            return AppDomain.CreateDomain("Temporary domain", null, setup);
        }    

        static void UnloadDomain(AppDomain domain)
        {            
            AppDomain.Unload(domain);
        }
    }
}

所以在 Main() 期间 Console.ReadKey(); 程序集仍被锁定,我无法访问它(例如,无法通过 Windows 删除它)。

有没有办法解决这个问题?

4

2 回答 2

0

我认为domain.Load正在将程序集连接到您的程序尝试加载内部Extension1尝试移动GetTypes到类Extension1

我不记得了,但我认为domain.Load只是运行Assembly.LoadFrom(,这就是将您的应用程序连接到 DLL 的原因。

于 2014-12-08T11:27:26.833 回答
0

好的,我解决了这个问题。我使用了卷影副本,只是在另一个域中配置了卷影副本,它对我有用。

于 2014-12-09T11:33:37.917 回答