日期:2014-05-18  浏览次数:20773 次

反射中的资源关闭问题
我写了一个自动更新程序。它包括两个功能。
功能一:文件替换。
功能二:数据库更新(采用反射来执行一个dll)。

其中反射部分的代码是这样写的。
  /// <summary>
  /// 通过反射,调用dll文件,执行数据库变更
  /// </summary>
  private void UpdateDataBase()
  {
  string strDllPath = Application.StartupPath + "\\ClassLibrary_YSJXC_Update.dll";
  Assembly ass = Assembly.LoadFile(strDllPath);
  Type type = ass.GetType("ClassLibrary_YSJXC_Update.ClassUpdate");
  object obj = ass.CreateInstance("ClassLibrary_YSJXC_Update.ClassUpdate");

  string strPath = Application.StartupPath + "\\db\\fdajxc.mdb";
  MethodInfo method = type.GetMethod("UpdateProcedures");
  method.Invoke(obj, new string[] { strPath });
  }

为了保证文件与数据库的一致性,如果数据库更新失败,那么文件需要还原回来。
而问题就处在这里了。

...
try
{
  UpdateDataBase();
}
catch(Exception ex)
{
  //还原文件
  ReBackSourceFiles();
}
..

由于要还原的文件ClassLibrary_YSJXC_Update,被载入了反射中,所以会报错。
我想是不是反射的资源没有被关闭。
请问:如何关闭反射资源?


------解决方案--------------------
我也遇到了同样的问题,也没有解决。
------解决方案--------------------
网上有个例子:

在C++中加载和卸载DLL是一件很容易的事,LoadLibrary和FreeLibrary让你能够轻易的在程序中加载DLL,然后在任何地方卸载。在C#中我们也能使用Assembly.LoadFile实现动态加载DLL,但是当你试图卸载时,你会很惊讶的发现Assembly没有提供任何卸载的方法。这是由于托管代码的自动垃圾回收机制会做这件事情,所以C#不提供释放资源的函数,一切由垃圾回收来做。
当AppDomain被卸载的时候,在该环境中的所有资源也将被回收。关于AppDomain的详细资料参考MSDN。下面是使用AppDomain实现动态卸载DLL的代码.
C# code
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Reflection;
namespace UnloadDll
{
    class Program
    {
        static void Main(string[] args)
        {
            string callingDomainName = AppDomain.CurrentDomain.FriendlyName;//Thread.GetDomain().FriendlyName;
            Console.WriteLine(callingDomainName);
            AppDomain ad = AppDomain.CreateDomain("DLL Unload test");
            ProxyObject obj = (ProxyObject)ad.CreateInstanceFrom_andUnwrap(@"UnloadDll.exe", "UnloadDll.ProxyObject");
            obj.LoadAssembly();
            obj.Invoke("TestDll.Class1", "Test", "It's a test");
            AppDomain.Unload(ad);
            obj = null;
            Console.ReadLine();
        }
    }
    class ProxyObject : MarshalByRefObject
    {
        Assembly assembly = null;
        public void LoadAssembly()
        {
            assembly = Assembly.LoadFile(@"TestDLL.dll");            
        }
        public bool Invoke(string fullClassName, string methodName, params Object[] args)
        {
            if(assembly == null)
                return false;
            Type tp = assembly.GetType(fullClassName);
            if (tp == null)
                return false;
            MethodInfo method = tp.GetMethod(methodName);
            if (method == null) 
                return false;
            Object obj = Activator.CreateInstance(tp);
            method.Invoke(obj, args);
            return true;            
        }
    }
}

------解决方案--------------------
应该是没有卸载的问题,4楼的方法不知道你试验了没
------解决方案--------------------
学习ing
------解决方案--------------------
哦!学习中,我只学过JAVA的反射
------解决方案--------------------
路过围观
------解决方案--------------------
收藏,
------解决方案--------------------