using System; using System.Reflection; namespace Loader { // container for assembly and exposes a GetObject function // to create a late-bound object for casting by the consumer // this class is meant to be contained in a separate appDomain // controlled by ObjectLoader class to allow for proper encapsulation // which enables proper shadow-copying functionality. internal class AssemblyLoader : MarshalByRefObject, IDisposable { #region class-level declarations private Assembly a = null; #endregion #region constructors and destructors public AssemblyLoader( string fullPath ){ if( a == null ){ a = Assembly.LoadFrom( fullPath ); } } ~AssemblyLoader(){ dispose( false ); } public void Dispose(){ dispose( true ); } private void dispose( bool disposing ){ if( disposing ){ a = null; System.GC.Collect(); System.GC.WaitForPendingFinalizers(); System.GC.Collect( 0 ); } } #endregion #region public functionality public object GetObject( string typename, object[] ctorParms ){ BindingFlags flags = BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public; object o = null; if( a != null ){ try{ o = a.CreateInstance( typename, true, flags, null, ctorParms, null, null ); } catch (Exception){ o = new ObjectLoadFailureException(); } } else { o = new AssemblyNotLoadedException(); } return o; } public object GetObject( string typename ){ return GetObject( typename, null ); } #endregion } }
--- end cut
相关的一些资源:
Why isn't there an Assembly.Unload method?
AppDomains ("application domains")
到此这篇关于C# 有关Assembly.Unload详解的文章就介绍到这了,更多相关C# 有关Assembly.Unload内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章: