共用方式為


Variant Structure

Represents an object that can be interpreted as more than one type.

This API is not CLS-compliant. The CLS-compliant alternative is [None].

Namespace:  Microsoft.VisualStudio.Package
Assemblies:   Microsoft.VisualStudio.Package.LanguageService (in Microsoft.VisualStudio.Package.LanguageService.dll)
  Microsoft.VisualStudio.Package.LanguageService.10.0 (in Microsoft.VisualStudio.Package.LanguageService.10.0.dll)
  Microsoft.VisualStudio.Package.LanguageService.12.0 (in Microsoft.VisualStudio.Package.LanguageService.12.0.dll)
  Microsoft.VisualStudio.Package.LanguageService.11.0 (in Microsoft.VisualStudio.Package.LanguageService.11.0.dll)
  Microsoft.VisualStudio.Package.LanguageService.9.0 (in Microsoft.VisualStudio.Package.LanguageService.9.0.dll)

Syntax

'Declaration
<CLSCompliantAttribute(False)> _
Public Structure Variant
[CLSCompliantAttribute(false)]
public struct Variant
[CLSCompliantAttribute(false)]
public value class Variant
[<Sealed>]
[<CLSCompliantAttribute(false)>]
type Variant =  struct end
JScript supports the use of structures, but not the declaration of new ones.

The Variant type exposes the following members.

Properties

  Name Description
Public property Value Determines the value of this Variant object.
Public property Vt Determines the type of the Variant object.

Top

Methods

  Name Description
Public method Equals Indicates whether this instance and a specified object are equal. (Inherited from ValueType.)
Public method GetHashCode Returns the hash code for this instance. (Inherited from ValueType.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method ToChar Interprets the Variant object's value as a Unicode character if the variant type is VT_UI2.
Public method ToString Returns the fully qualified type name of this instance. (Inherited from ValueType.)
Public methodStatic member ToVariant Converts the given variant object to a Variant class object.

Top

Remarks

This version of the variant structure is specific to the managed package framework (MPF) language service classes and is designed to primarily convert a variant to a character. More specifically, if the variant has the type VT_UI2, the variant object is converted to a character.

This structure is actually used in only one place in the MPF language service classes, the HandlePreExec method of the ViewFilter class. This structure is used to convert the incoming variant to a character if the type of command is TYPECHAR.

Notes to Implementers

This is a limited structure and there is nothing to override.

Notes to Callers

This is a structure and can be treated as a variable type. It has limited functionality, mainly designed to convert from a variant object to a character.

Examples

This example shows how the MPF ViewFilter class uses this Variant structure to convert an incoming variant object to a character.

namespace Microsoft.VisualStudio.Package
{
    [CLSCompliant(false)]
    [System.Runtime.InteropServices.ComVisible(true)]
    public class ViewFilter 
        : IVsTextViewFilter
        , IVsTextViewEvents
        , IOleCommandTarget
        , IDisposable
    {
        public virtual void HandlePostExec(ref Guid guidCmdGroup,
                                           uint nCmdId,
                                           uint nCmdexecopt,
                                           IntPtr pvaIn,
                                           IntPtr pvaOut)
        { 

            if (guidCmdGroup == VsMenus.guidStandardCommandSet2K) {
                VsCommands2K cmd = (VsCommands2K)nCmdId;
                char ch = '\0';
                if (cmd == VsCommands2K.TYPECHAR && pvaIn != IntPtr.Zero) {
                    Variant v = Variant.ToVariant(pvaIn);
                    ch = v.ToChar();

                    // Do something with the character

                }
            }
        }
    }
}

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

Microsoft.VisualStudio.Package Namespace