VariantWrapper 類別



此 API 現已淘汰。

VT_VARIANT | VT_BYREF 型別的資料從 Managed 封送處理成 Unmanaged 程式碼。Marshals data of type VT_VARIANT | VT_BYREF from managed to unmanaged code. 此類別無法被繼承。This class cannot be inherited.

public ref class VariantWrapper sealed
[System.Obsolete("VariantWrapper and support for marshalling to the VARIANT type may be unavailable in future releases.")]
public sealed class VariantWrapper
type VariantWrapper = class
Public NotInheritable Class VariantWrapper


下列程式碼範例示範如何使用 VariantWrapper 類別來包裝 interop 封送處理器傳遞為 VT_VARIANT | VT_BYREFObjectThe following code example demonstrates how to use the VariantWrapper class to wrap an Object that the interop marshaler passes as VT_VARIANT | VT_BYREF.

// Create an instance of an unmanged COM object.
UnmanagedComClass UnmanagedComClassInstance = new UnmanagedComClass();

// Create a string to pass to the COM object.
string helloString = "Hello World!";

// Wrap the string with the VariantWrapper class.
object var = new System.Runtime.InteropServices.VariantWrapper(helloString);

// Pass the wrapped object.
UnmanagedComClassInstance.MethodWithStringRefParam(ref var);
' Create an instance of an unmanged COM object.
Dim UnmanagedComClassInstance As New UnmanagedComClass()

' Create a string to pass to the COM object.
Dim helloString As String = "Hello World!"

' Wrap the string with the VariantWrapper class.
Dim var As Object = New System.Runtime.InteropServices.VariantWrapper(helloString)

' Pass the wrapped object.


VariantWrapper 的目的是要在將 managed 類型封送處理至對應的 VARIANT 類型時,加入一個間接取值層級。The purpose of VariantWrapper is to add one level of indirection when marshaling a managed type to the corresponding VARIANT type.

您可以使用這個類別來包裝 interop 封送處理器傳遞為 VT_VARIANT | VT_BYREFObjectYou can use this class to wrap an Object that the interop marshaler passes as VT_VARIANT | VT_BYREF. 在 .NET Framework 的版本1.0 和1.1 中,無法將類型 VT_VARIANT | VT_BYREF 的 variant 資料封送處理至未受管理的程式碼。In versions 1.0 and 1.1 of the .NET Framework, it was not possible to marshal variant data of type VT_VARIANT | VT_BYREF to unmanaged code. Interop 封送處理器傳遞了 managed 類型的變體(例如,StringVT_BSTR | VT_BYREF,或 Int32VT_I4 | VT_BYREF),但不會 VT_VARIANT | VT_BYREFThe interop marshaler passed a variant of the managed type (for example, VT_BSTR | VT_BYREF for String, or VT_I4 | VT_BYREF for Int32), but not VT_VARIANT | VT_BYREF.

使用 VT_VARIANT | VT_BYREF variant 類型的其中一項優點是,可以在方法呼叫期間變更資料類型。One advantage of using VT_VARIANT | VT_BYREF variant types is that the type of data can be changed during a method call. 例如,您可以傳遞包含 VT_BSTRVT_VARIANT | VT_BYREF variant 型別,並取得在方法呼叫之後傳回包含 VT_I4 的 variant。For example, you can pass a VT_VARIANT | VT_BYREF variant type that contains a VT_BSTR and get a variant returned that contains a VT_I4 after a method call. 由於 COM Interop 封送處理器無法得知何時要傳遞 VT_BSTR | VT_BYREF,而當傳遞 VT_VARIANT | VT_BYREF時,會指向包含宣告為 VARIANT *之參數 BSTR 的變數,您可以使用 VariantWrapper來指示封送處理器。Because the COM interop marshaler has no way of knowing when to pass VT_BSTR | VT_BYREF and when to pass VT_VARIANT | VT_BYREF, which points to a variant that contains a BSTR for parameters declared as VARIANT *, you can instruct the marshaler by using VariantWrapper.

請注意,不支援早期繫結;只有在呼叫 InvokeMember 或使用以早期繫結方式呼叫的分派介面時,才能使用 VariantWrapperNote that early binding is not supported; you can use VariantWrapper only when calling InvokeMember or with a Dispatch-only interface called in an early bound fashion. 在C#中,您也必須使用 ref 關鍵字,為 VariantWrapper類型的任何參數指定 ByRef 的語義。In C#, you must also use the ref keyword to specify ByRef semantics for any parameter of type VariantWrapper. 在 Visual Basic 中,會針對每個隱含的晚期繫結呼叫自動加入 ByRef 的語義。In Visual Basic, ByRef semantics are added automatically for every implicit late binding call. 另請注意,不支援 VariantWrapper 物件的嵌套 VariantWrapper 物件和陣列。Also note that nesting VariantWrapper objects and arrays of VariantWrapper objects is not supported.



為指定的 VariantWrapper 參數,初始化 Object 類別的新執行個體。Initializes a new instance of the VariantWrapper class for the specified Object parameter.



取得 VariantWrapper 物件所包裝的物件。Gets the object wrapped by the VariantWrapper object.



判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)