Olvasás angol nyelven Szerkesztés

Megosztás a következőn keresztül:


VariantWrapper Class

Definition

Caution

VariantWrapper and support for marshalling to the VARIANT type may be unavailable in future releases.

Marshals data of type VT_VARIANT | VT_BYREF from managed to unmanaged code. This class cannot be inherited.

[System.Obsolete("VariantWrapper and support for marshalling to the VARIANT type may be unavailable in future releases.")]
public sealed class VariantWrapper
public sealed class VariantWrapper
[System.Serializable]
public sealed class VariantWrapper
Inheritance
VariantWrapper
Attributes

Examples

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);

Remarks

The purpose of VariantWrapper is to add one level of indirection when marshaling a managed type to the corresponding VARIANT type.

You can use this class to wrap an Object that the interop marshaler passes as 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. 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.

One advantage of using VT_VARIANT | VT_BYREF variant types is that the type of data can be changed during a method call. 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. 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.

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. In C#, you must also use the ref keyword to specify ByRef semantics for any parameter of type VariantWrapper. In Visual Basic, ByRef semantics are added automatically for every implicit late binding call. Also note that nesting VariantWrapper objects and arrays of VariantWrapper objects is not supported.

Constructors

VariantWrapper(Object)

Initializes a new instance of the VariantWrapper class for the specified Object parameter.

Properties

WrappedObject

Gets the object wrapped by the VariantWrapper object.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

Termék Verziók (Elavult)
.NET 5, 6, 7, 8, 9 (Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 (4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided))
.NET Standard 1.1, 2.0 (1.2, 1.3, 1.4, 1.5, 1.6, 2.1)
UWP (10.0)