TypeId Property
Collapse the table of content
Expand the table of content

Attribute.TypeId Property

When implemented in a derived class, gets a unique identifier for this Attribute.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

'Declaration
Public Overridable ReadOnly Property TypeId As Object
'Usage
Dim instance As Attribute
Dim value As Object

value = instance.TypeId

/** @property */
public Object get_TypeId ()

public function get TypeId () : Object

Not applicable.

Property Value

An Object that is a unique identifier for the attribute.

As implemented, this identifier is merely the Type of the attribute. However, it is intended that the unique identifier be used to identify two attributes of the same type.

The following code example implements the TypeId property in a custom parameter Attribute class and shows its use.

' Example for the Attribute.TypeId property.
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

Namespace NDP_UE_VB

    ' Define a custom parameter attribute that takes a single message argument.
    <AttributeUsage(AttributeTargets.Parameter)>  _
    Public Class ArgumentUsageAttribute
        Inherits Attribute
           
        ' The constructor saves the message and creates a unique identifier.
        Public Sub New(UsageMsg As String)
            Me.usageMsg = UsageMsg
            Me.GUIDinstance = Guid.NewGuid()
        End Sub ' New

        ' This is storage for the attribute message and unique ID.
        Protected usageMsg As String
        Protected GUIDinstance As Guid
           
        ' This is the Message property for the attribute.
        Public Property Message() As String
            Get
                Return usageMsg
            End Get
            Set
                usageMsg = value
            End Set
        End Property 

        ' Override TypeId to provide a unique identifier for the instance.
        Public Overrides ReadOnly Property TypeId() As Object
            Get
                Return CType(GUIDinstance, Object)
            End Get
        End Property
            
        ' Override ToString() to append the message to what base the generates.
        Public Overrides Function ToString() As String
            Return MyBase.ToString() + ":" + usageMsg
        End Function ' ToString
    End Class ' ArgumentUsageAttribute

    Public Class TestClass
       
        ' Assign an ArgumentUsage attribute to each parameter.
        ' Assign a ParamArray attribute to strList.
        Public Sub TestMethod( _
            <ArgumentUsage("Must pass an array here.")> _
            strArray() As String, _
            <ArgumentUsage("Can pass a param list or array here.")> _
            ParamArray strList() As String)
        End Sub ' TestMethod
    End Class ' TestClass

    Module AttributeTypeIdDemo
       
        ' Create attributes from the derived class, 
        ' and then display the TypeId values.
        Sub ShowAttributeTypeIds()

            ' Get the class type, and then get the MethodInfo object 
            ' for TestMethod to access its metadata.
            Dim clsType As Type = GetType(TestClass)
            Dim mInfo As MethodInfo = clsType.GetMethod("TestMethod")
              
            ' There will be two elements in pInfoArray, one for each parameter.
            Dim pInfoArray As ParameterInfo() = mInfo.GetParameters()
            If Not (pInfoArray Is Nothing) Then

                ' Create an instance of the param array attribute on strList.
                Dim listArrayAttr As ParamArrayAttribute = _
                    Attribute.GetCustomAttribute(pInfoArray(1), _
                        GetType(ParamArrayAttribute))
                 
                ' Create an instance of the argument usage attribute on strArray.
                Dim arrayUsageAttr1 As ArgumentUsageAttribute = _
                    Attribute.GetCustomAttribute(pInfoArray(0), _
                        GetType(ArgumentUsageAttribute))
                 
                ' Create another instance of the argument usage attribute 
                ' on strArray.
                Dim arrayUsageAttr2 As ArgumentUsageAttribute = _
                    Attribute.GetCustomAttribute(pInfoArray(0), _
                        GetType(ArgumentUsageAttribute))
                 
                ' Create an instance of the argument usage attribute on strList.
                Dim listUsageAttr As ArgumentUsageAttribute = _
                    Attribute.GetCustomAttribute(pInfoArray(1), _
                        GetType(ArgumentUsageAttribute))
                 
                ' Display the attributes and corresponding TypeId values.
                Console.WriteLine(vbCrLf & """{0}"" " & vbCrLf & "TypeId: {1}", _
                    listArrayAttr.ToString(), listArrayAttr.TypeId)
                Console.WriteLine(vbCrLf & """{0}"" " & vbCrLf & "TypeId: {1}", _
                    arrayUsageAttr1.ToString(), arrayUsageAttr1.TypeId)
                Console.WriteLine(vbCrLf & """{0}"" " & vbCrLf & "TypeId: {1}", _
                    arrayUsageAttr2.ToString(), arrayUsageAttr2.TypeId)
                Console.WriteLine(vbCrLf & """{0}"" " & vbCrLf & "TypeId: {1}", _
                    listUsageAttr.ToString(), listUsageAttr.TypeId)
            Else
                Console.WriteLine("The parameters information could not " & _
                    "be retrieved for method {0}.", mInfo.Name)
            End If
        End Sub ' ShowAttributeTypeIds

        Sub Main()
            Console.WriteLine( _
                "This example of the Attribute.TypeId property" & _
                vbCrLf & "generates the following output.")
            Console.WriteLine( _
                vbCrLf & "Create instances from a derived Attribute " & _
                "class that implements TypeId, " & vbCrLf & "and then " & _
                "display the attributes and corresponding TypeId values:" )

            ShowAttributeTypeIds( )
        End Sub ' Main

    End Module ' AttributeTypeIdDemo
End Namespace ' NDP_UE_VB

' This example of the Attribute.TypeId property
' generates the following output.
' 
' Create instances from a derived Attribute class that implements TypeId,
' and then display the attributes and corresponding TypeId values:
' 
' "System.ParamArrayAttribute"
' TypeId: System.ParamArrayAttribute
' 
' "NDP_UE_VB.ArgumentUsageAttribute:Must pass an array here."
' TypeId: f312e528-3ff9-4587-9e6d-8108b62f2980
' 
' "NDP_UE_VB.ArgumentUsageAttribute:Must pass an array here."
' TypeId: 7b2cf0ec-b166-4557-a7ab-137a57c87226
' 
' "NDP_UE_VB.ArgumentUsageAttribute:Can pass a param list or array here."
' TypeId: 0b05f2a7-4a15-4d24-99f0-8503b238a18c

// Example for the Attribute.TypeId property.

package NDP_UE_JSL;

import System.*;
import System.Reflection.*;

// Define a custom parameter attribute that takes a single message argument.
/** @attribute AttributeUsage(AttributeTargets.Parameter)
 */
public class ArgumentUsageAttribute extends Attribute
{
    // The constructor saves the message and creates a unique identifier.
    public ArgumentUsageAttribute(String usgMsg)
    {
        this.usageMsg = usgMsg;
        this.instanceGUID = Guid.NewGuid();
    } //ArgumentUsageAttribute

    // This is storage for the attribute message and unique ID.
    protected String usageMsg;
    protected Guid instanceGUID;

    // This is the Message property for the attribute.
    /** @property 
     */
    public String get_Message()
    {
        return usageMsg;
    } //get_Message

    /** @property 
     */
    public void set_Message(String value)
    {
        usageMsg = value;
    } //set_Message

    // Override TypeId to provide a unique identifier for the instance.
    /** @property 
     */
    public Object get_TypeId()
    {
        return (Object)instanceGUID;
    } //get_TypeId

    // Override ToString() to append the message to what the base generates.
    public String ToString()
    {
        return super.ToString() + ":" + usageMsg;
    } //ToString
} //ArgumentUsageAttribute

public class TestClass
{
    // Assign an ArgumentUsage attribute to each parameter.
    public void TestMethod(
        /** @attribute ArgumentUsage("Must pass an array here.")
        */
        String strArray[],
        /** @attribute ArgumentUsage("Can pass a param list or array here.")
         */
        /** @attribute System.ParamArray() 
         */
        String strList[])
    {
    } //TestMethod
} //TestClass

class AttributeTypeIdDemo
{
    static void ShowAttributeTypeIds()
    {
        // Get the class type, and then get the MethodInfo object 
        // for TestMethod to access its metadata.
        Type clsType = TestClass.class.ToType();
        MethodInfo mInfo = clsType.GetMethod("TestMethod");
        // There will be two elements in pInfoArray, one for each parameter.
        ParameterInfo pInfoArray[] = mInfo.GetParameters();

        if (pInfoArray != null) {
            // Create an instance of the param array attribute on strList.
            ParamArrayAttribute listArrayAttr = (ParamArrayAttribute)(
                Attribute.GetCustomAttribute(pInfoArray[1],
                ParamArrayAttribute.class.ToType()));
            // Create an instance of the argument usage attribute on strArray.
            ArgumentUsageAttribute arrayUsageAttr1 = (ArgumentUsageAttribute)(
                Attribute.GetCustomAttribute(pInfoArray[0], 
                ArgumentUsageAttribute.class.ToType()));
            // Create another instance of the argument usage attribute 
            // on strArray.
            ArgumentUsageAttribute arrayUsageAttr2 = (ArgumentUsageAttribute)(
                Attribute.GetCustomAttribute(pInfoArray[0], 
                ArgumentUsageAttribute.class.ToType()));
            // Create an instance of the argument usage attribute on strList.
            ArgumentUsageAttribute listUsageAttr = (ArgumentUsageAttribute)(
                Attribute.GetCustomAttribute(pInfoArray[1], 
                ArgumentUsageAttribute.class.ToType()));
            // Display the attributes and corresponding TypeId values.
            Console.WriteLine("\n\"{0}\" \nTypeId: {1}", 
                listArrayAttr.ToString(), listArrayAttr.get_TypeId());
            Console.WriteLine("\n\"{0}\" \nTypeId: {1}", 
                arrayUsageAttr1.ToString(), arrayUsageAttr1.get_TypeId());
            Console.WriteLine("\n\"{0}\" \nTypeId: {1}", 
                arrayUsageAttr2.ToString(), arrayUsageAttr2.get_TypeId());
            Console.WriteLine("\n\"{0}\" \nTypeId: {1}", 
                listUsageAttr.ToString(), listUsageAttr.get_TypeId());
        }
        else {
            Console.WriteLine("The parameters information could " 
                + "not be retrieved for method {0}.", mInfo.get_Name());
        }
    } //ShowAttributeTypeIds

    public static void main(String[] args)
    {
        Console.WriteLine("This example of the Attribute.TypeId property\n" 
            + "generates the following output.");
        Console.WriteLine("\nCreate instances from a derived Attribute " 
            + "class that implements TypeId, \nand then " 
            + "display the attributes and corresponding TypeId values:");
        ShowAttributeTypeIds();
    } //main
} //AttributeTypeIdDemo

/*
    This example of the Attribute.TypeId property
    generates the following output.

    Create instances from a derived Attribute class that implements TypeId,
    and then display the attributes and corresponding TypeId values:

    "System.ParamArrayAttribute"
    TypeId: System.ParamArrayAttribute

    "NDP_UE_JSL.ArgumentUsageAttribute@c10548ee:Must pass an array here."
    TypeId: b67bef05-6ef3-4583-876b-99c38b6b662a
    
    "NDP_UE_JSL.ArgumentUsageAttribute@c10548ee:Must pass an array here."
    TypeId: 5bf96750-d43c-457f-8210-802abdd37208
    
    "NDP_UE_JSL.ArgumentUsageAttribute@419634a1:Can pass a param list or array 
    here."
    TypeId: 2e4fb470-ede6-4c45-b7ab-15bd7545f556
*/

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2016 Microsoft