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


下面的代码示例演示如何使用VariantWrapperObject包装互操作封送拆收器传递VT_VARIANT | VT_BYREF的。The 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_BYREF传递的。You 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例如String, 适用VT_I4 | VT_BYREFInt32或), 而VT_VARIANT | VT_BYREF不是。The 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(指向为声明为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.

请注意, 不支持早期绑定;仅当调用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关键字为类型VariantWrapperByRef的任何参数指定语义。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) 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)