VariantWrapper VariantWrapper VariantWrapper VariantWrapper Class



此 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类用于包装Object的互操作封送处理程序将作为传递VT_VARIANT | VT_BYREFThe 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.

您可以使用此类包装Object的互操作封送处理程序将作为传递VT_VARIANT | VT_BYREFYou can use this class to wrap an Object that the interop marshaler passes as VT_VARIANT | VT_BYREF. 在版本 1.0 和 1.1 版.NET Framework 中,它无法封送 variant 数据类型的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有关String,或VT_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_VARIANT | VT_BYREF包含的变体类型VT_BSTR和包含的获取变体返回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,它指向一个变量,包含该BSTR的参数声明为VARIANT *,可以通过使用指示封送处理程序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.

请注意,不支持早期绑定;可以使用VariantWrapper仅当调用InvokeMember或具有早期绑定方式调用仅支持调度的接口。Note 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关键字来指定ByRef类型的任何参数的语义VariantWrapperIn 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) VariantWrapper(Object) VariantWrapper(Object) VariantWrapper(Object)

使用指定的 Object 参数初始化 VariantWrapper 类的新实例。Initializes a new instance of the VariantWrapper class for the specified Object parameter.


WrappedObject WrappedObject WrappedObject WrappedObject

获取由 VariantWrapper 对象包装的对象。Gets the object wrapped by the VariantWrapper object.


Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)