RuntimeCompatibilityAttribute 類別

定義

指定是否使用 Exception 物件來包裝不是衍生自 RuntimeWrappedException 類別的例外狀況。 此類別無法獲得繼承。

public ref class RuntimeCompatibilityAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=false, Inherited=false)]
public sealed class RuntimeCompatibilityAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=false, Inherited=false)]
[System.Serializable]
public sealed class RuntimeCompatibilityAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=false, Inherited=false)>]
type RuntimeCompatibilityAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=false, Inherited=false)>]
[<System.Serializable>]
type RuntimeCompatibilityAttribute = class
    inherit Attribute
Public NotInheritable Class RuntimeCompatibilityAttribute
Inherits Attribute
繼承
RuntimeCompatibilityAttribute
屬性

範例

下列程式碼範例示範如何將 類別套用 RuntimeCompatibilityAttribute 至在 C++ 中擲回 String 物件做為例外狀況的元件,並使用 物件攔截它 RuntimeWrappedException

using namespace System;
using namespace System::Runtime::CompilerServices;

[assembly:RuntimeCompatibilityAttribute(WrapNonExceptionThrows = true)]; 

void run()
{
    try
    {
        throw gcnew String("This is a string");

    }
    catch(RuntimeWrappedException^ e)
    {
        Console::WriteLine("RuntimeWrappedException caught!");
    }
}

int main()
{
    run();

    return 0;
}

備註

某些語言,例如 C++,可讓您擲回任何類型的例外狀況。 其他語言,例如 Microsoft C# 和 Visual Basic,要求每個擲回的 Exception 例外狀況都衍生自 類別。 為了維護語言之間的相容性,Common Language Runtime (CLR) 包裝不是衍生自 Exception 物件的物件 RuntimeWrappedException

您可以使用 類別 RuntimeCompatibilityAttribute 來指定例外狀況是否應該包裝在元件的 catch 區塊和例外狀況篩選內。 許多語言編譯器,包括 Microsoft C# 和 Visual Basic 編譯器,預設會套用此屬性來指定包裝行為。

請注意,即使您使用 RuntimeCompatibilityAttribute 類別來指定不要包裝例外狀況,執行時間仍會包裝例外狀況。 在此情況下,例外狀況只會在 catch 區塊或例外狀況篩選內解除包裝。

建構函式

RuntimeCompatibilityAttribute()

初始化 RuntimeCompatibilityAttribute 類別的新執行個體。

屬性

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)
WrapNonExceptionThrows

取得或設定值,指出是否使用 Exception 物件來包裝不是衍生自 RuntimeWrappedException 類別的例外狀況。

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於