VariantWrapper 类



此 API 现已过时。

VT_VARIANT | VT_BYREF 类型的数据从托管代码封送到非托管代码。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 类包装互操作封送拆收器作为 VT_VARIANT | VT_BYREF传递的 ObjectThe 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 的目的是在将托管类型封送到相应的 VARIANT 类型时添加一个级别的间接寻址。The purpose of VariantWrapper is to add one level of indirection when marshaling a managed type to the corresponding VARIANT type.

此类可用于包装互操作封送拆收器作为 VT_VARIANT | VT_BYREF传递的 ObjectYou 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 类型的变体数据封送到非托管代码。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. 互操作封送拆收器传递了托管类型的变体(例如,VT_BSTR | VT_BYREF 用于 StringVT_I4 | VT_BYREF Int32),但不是 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 变量类型的一个优点是可以在方法调用期间更改数据的类型。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 变体类型,并在方法调用后获取包含 VT_I4 的返回变量。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 互操作封送拆收器无法知道何时传递 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)

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)