CannotUnloadAppDomainException 类

卸载应用程序域的尝试失败时引发的异常。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class CannotUnloadAppDomainException
    Inherits SystemException
用法
Dim instance As CannotUnloadAppDomainException
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class CannotUnloadAppDomainException : SystemException
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class CannotUnloadAppDomainException : public SystemException
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class CannotUnloadAppDomainException extends SystemException
SerializableAttribute 
ComVisibleAttribute(true) 
public class CannotUnloadAppDomainException extends SystemException

备注

尝试卸载下列对象时引发的 CannotUnloadAppDomainException

  • 默认应用程序域,该域在应用程序的生存期内必须保持加载状态。

  • 包含无法立即停止执行的运行线程的应用程序域。

  • 已经被卸载的应用程序域。

CannotUnloadAppDomainException 使用值为 0x80131015 的 HRESULT COR_E_CANNOTUNLOADAPPDOMAIN。

有关 CannotUnloadAppDomainException 实例的初始属性值列表,请参见 CannotUnloadAppDomainException 构造函数。

示例

Imports System
Imports System.Reflection
Imports System.Security.Policy
'Imports System.Data
 'for evidence object

Class ADMultiDomain
   
   ' The following attribute indicates to loader that multiple application 
   ' domains are used in this application.
   <LoaderOptimizationAttribute(LoaderOptimization.MultiDomainHost)>  _
   Public Shared Sub Main()
      ' Create application domain setup information for new application domain.
      Dim domaininfo As New AppDomainSetup()
      domaininfo.ApplicationBase = System.Environment.CurrentDirectory
      domaininfo.ApplicationName = "MyMultiDomain Application"
      
      'Create evidence for the new appdomain from evidence of current application domain.
      Dim adevidence As Evidence = AppDomain.CurrentDomain.Evidence
      
      ' Create appdomain.
      Dim newDomain As AppDomain = AppDomain.CreateDomain("MyMultiDomain", adevidence, domaininfo)
      
      'Load an assembly into the new application domain.
      Dim w As Worker = CType( _
         newDomain.CreateInstanceAndUnwrap( _
            [Assembly].GetExecutingAssembly().GetName().Name, _
            "Worker"), _
         Worker) 
      w.TestLoad()
      
      'Unload the application domain, which also unloads the assembly.
      AppDomain.Unload(newDomain)
      
   End Sub 'Main
End Class 'ADMultiDomain

Class Worker
   Inherits MarshalByRefObject

   Friend Sub TestLoad()
      ' You must supply a valid assembly display name here.
      [Assembly].Load("Text assembly name, Culture, PublicKeyToken, Version")

      For Each assem As [Assembly] In AppDomain.CurrentDomain.GetAssemblies()
         Console.WriteLine(assem.FullName)
      Next
   End Sub
End Class
using System;
using System.Reflection;
using System.Security.Policy;  //for evidence object

class ADMultiDomain
{
   // The following attribute indicates to loader that multiple application 
   // domains are used in this application.
   [LoaderOptimizationAttribute( LoaderOptimization.MultiDomainHost)]
   public static void Main()
   {
      // Create application domain setup information for new application domain.
      AppDomainSetup domaininfo = new AppDomainSetup();
      domaininfo.ApplicationBase = System.Environment.CurrentDirectory;
      domaininfo.ApplicationName = "MyMultiDomain Application";

      //Create evidence for the new appdomain from evidence of current application domain.
      Evidence adevidence = AppDomain.CurrentDomain.Evidence;

      // Create appdomain.
      AppDomain newDomain = AppDomain.CreateDomain("MyMultiDomain", adevidence, domaininfo);

      // Load an assembly into the new application domain.
      Worker w = (Worker) newDomain.CreateInstanceAndUnwrap( 
         Assembly.GetExecutingAssembly().GetName().Name,
         "Worker"
      );
      w.TestLoad();

      //Unload the application domain, which also unloads the assembly.
      AppDomain.Unload(newDomain);
   }
}

class Worker : MarshalByRefObject
{
   internal void TestLoad()
   {
      // You must supply a valid fully qualified assembly name here.
      Assembly.Load("Text assembly name, Culture, PublicKeyToken, Version");
      foreach (Assembly assem in AppDomain.CurrentDomain.GetAssemblies())
         Console.WriteLine(assem.FullName);
   }
}
using namespace System;
using namespace System::Reflection;
using namespace System::Security::Policy;

ref class Worker : MarshalByRefObject
{
public:
   void TestLoad()
   {
      // You must supply a valid fully qualified assembly name here.
      Assembly::Load("Text assembly name, Culture, PublicKeyToken, Version");
      for each (Assembly^ assem in AppDomain::CurrentDomain->GetAssemblies())
         Console::WriteLine(assem->FullName);
   }
};

//for evidence Object*
// The following attribute indicates to loader that multiple application
// domains are used in this application.

[LoaderOptimizationAttribute(LoaderOptimization::MultiDomainHost)]
int main()
{
   
   // Create application domain setup information for new application domain.
   AppDomainSetup^ domaininfo = gcnew AppDomainSetup;
   domaininfo->ApplicationBase = System::Environment::CurrentDirectory;
   domaininfo->ApplicationName = "MyMultiDomain Application";
   
   //Create evidence for the new appdomain from evidence of current application domain.
   Evidence^ adevidence = AppDomain::CurrentDomain->Evidence;
   
   // Create appdomain.
   AppDomain^ newDomain = AppDomain::CreateDomain( "MyMultiDomain", adevidence, domaininfo );
   
   // Load an assembly into the new application domain.
   Worker^ w = (Worker^) newDomain->CreateInstanceAndUnwrap(
      Assembly::GetExecutingAssembly()->GetName()->Name,
      "Worker"
   );
   w->TestLoad();

   //Unload the application domain, which also unloads the assembly.
   AppDomain::Unload(newDomain);
}

继承层次结构

System.Object
   System.Exception
     System.SystemException
      System.CannotUnloadAppDomainException

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

CannotUnloadAppDomainException 成员
System 命名空间
Object
Exception
AppDomain 类
Thread

其他资源

处理和引发异常