Attribute.TypeId Propriété

Définition

Lors de l'implémentation dans une classe dérivée, obtient un identificateur unique pour l'objet Attribute.

public:
 virtual property System::Object ^ TypeId { System::Object ^ get(); };
public virtual object TypeId { get; }
member this.TypeId : obj
Public Overridable ReadOnly Property TypeId As Object

Valeur de propriété

Object qui est un identificateur unique pour l'attribut.

Exemples

L’exemple de code suivant implémente la TypeId propriété dans une classe de paramètre Attribute personnalisée et montre son utilisation.

// Example for the Attribute::TypeId property.
using namespace System;
using namespace System::Reflection;

namespace NDP_UE_CPP
{

   // Define a custom parameter attribute that takes a single message argument.

   [AttributeUsage(AttributeTargets::Parameter)]
   public ref class ArgumentUsageAttribute: public Attribute
   {
   protected:

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

   public:

      // The constructor saves the message and creates a unique identifier.
      ArgumentUsageAttribute( String^ UsageMsg )
      {
         this->usageMsg = UsageMsg;
         this->instanceGUID = Guid::NewGuid();
      }

      property String^ Message 
      {
         // This is the Message property for the attribute.
         String^ get()
         {
            return usageMsg;
         }

         void set( String^ value )
         {
            this->usageMsg = value;
         }
      }

      property Object^ TypeId 
      {
         // Override TypeId to provide a unique identifier for the instance.
         virtual Object^ get() override
         {
            return instanceGUID;
         }
      }

      // Override ToString() to append the message to 
      // what the base generates.
      virtual String^ ToString() override
      {
         return String::Concat( Attribute::ToString(), ":", usageMsg );
      }
   };

   public ref class TestClass
   {
   public:

      // Assign an ArgumentUsage attribute to each parameter.
      // Assign a ParamArray attribute to strList.
      void TestMethod( [ArgumentUsage("Must pass an array here.")]array<String^>^strArray,
                       [ArgumentUsage("Can pass a param list or array here.")]array<String^>^strList ){}
   };

   static void ShowAttributeTypeIds()
   {
      // Get the class type, and then get the MethodInfo object 
      // for TestMethod to access its metadata.
      Type^ clsType = TestClass::typeid;
      MethodInfo^ mInfo = clsType->GetMethod( "TestMethod" );

      // There will be two elements in pInfoArray, one for each parameter.
      array<ParameterInfo^>^pInfoArray = mInfo->GetParameters();
      if ( pInfoArray != nullptr )
      {
         // Create an instance of the param array attribute on strList.
         ParamArrayAttribute^ listArrayAttr = static_cast<ParamArrayAttribute^>(Attribute::GetCustomAttribute( pInfoArray[ 1 ], ParamArrayAttribute::typeid ));

         // Create an instance of the argument usage attribute on strArray.
         ArgumentUsageAttribute^ arrayUsageAttr1 = static_cast<ArgumentUsageAttribute^>(Attribute::GetCustomAttribute( pInfoArray[ 0 ], ArgumentUsageAttribute::typeid ));

         // Create another instance of the argument usage attribute 
         // on strArray.
         ArgumentUsageAttribute^ arrayUsageAttr2 = static_cast<ArgumentUsageAttribute^>(Attribute::GetCustomAttribute( pInfoArray[ 0 ], ArgumentUsageAttribute::typeid ));

         // Create an instance of the argument usage attribute on strList.
         ArgumentUsageAttribute^ listUsageAttr = static_cast<ArgumentUsageAttribute^>(Attribute::GetCustomAttribute( pInfoArray[ 1 ], ArgumentUsageAttribute::typeid ));

         // Display the attributes and corresponding TypeId values.
         Console::WriteLine( "\n\"{0}\" \nTypeId: {1}", listArrayAttr->ToString(), listArrayAttr->TypeId );
         Console::WriteLine( "\n\"{0}\" \nTypeId: {1}", arrayUsageAttr1->ToString(), arrayUsageAttr1->TypeId );
         Console::WriteLine( "\n\"{0}\" \nTypeId: {1}", arrayUsageAttr2->ToString(), arrayUsageAttr2->TypeId );
         Console::WriteLine( "\n\"{0}\" \nTypeId: {1}", listUsageAttr->ToString(), listUsageAttr->TypeId );
      }
      else
            Console::WriteLine( "The parameters information could "
      "not be retrieved for method {0}.", mInfo->Name );
   }
}

int main()
{
   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:" );
   NDP_UE_CPP::ShowAttributeTypeIds();
}

/*
This example of the Attribute::TypeId property
generates output similar to the following:

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_CPP.ArgumentUsageAttribute:Must pass an array here."
TypeId: 9316015d-1219-4ce1-b317-e71efb23d42e

"NDP_UE_CPP.ArgumentUsageAttribute:Must pass an array here."
TypeId: ebc1ba23-2573-4c1f-aea6-90515e733796

"NDP_UE_CPP.ArgumentUsageAttribute:Can pass a param list or array here."
TypeId: 624af10b-9bba-4403-a97e-46927e7385fb
*/
// Example for the Attribute.TypeId property.
using System;
using System.Reflection;

namespace NDP_UE_CS
{
    // Define a custom parameter attribute that takes a single message argument.
    [AttributeUsage( AttributeTargets.Parameter )]
    public class ArgumentUsageAttribute : Attribute
    {
        // The constructor saves the message and creates a unique identifier.
        public ArgumentUsageAttribute( string UsageMsg )
        {
            this.usageMsg = UsageMsg;
            this.instanceGUID = Guid.NewGuid( );
        }

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

        // This is the Message property for the attribute.
        public string Message
        {
            get { return usageMsg; }
            set { usageMsg = value; }
        }

        // Override TypeId to provide a unique identifier for the instance.
        public override object TypeId
        {
            get { return (object)instanceGUID; }
        }

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

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

    class AttributeTypeIdDemo
    {
        static void ShowAttributeTypeIds( )
        {
            // Get the class type, and then get the MethodInfo object
            // for TestMethod to access its metadata.
            Type clsType = typeof( TestClass );
            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],
                        typeof(ParamArrayAttribute) );

                // Create an instance of the argument usage attribute on strArray.
                ArgumentUsageAttribute arrayUsageAttr1 = (ArgumentUsageAttribute)
                    Attribute.GetCustomAttribute( pInfoArray[0],
                        typeof(ArgumentUsageAttribute) );

                // Create another instance of the argument usage attribute
                // on strArray.
                ArgumentUsageAttribute arrayUsageAttr2 = (ArgumentUsageAttribute)
                    Attribute.GetCustomAttribute( pInfoArray[0],
                        typeof(ArgumentUsageAttribute) );

                // Create an instance of the argument usage attribute on strList.
                ArgumentUsageAttribute listUsageAttr = (ArgumentUsageAttribute)
                    Attribute.GetCustomAttribute( pInfoArray[1],
                        typeof(ArgumentUsageAttribute) );

                // Display the attributes and corresponding TypeId values.
                Console.WriteLine( "\n\"{0}\" \nTypeId: {1}",
                    listArrayAttr.ToString(), listArrayAttr.TypeId );
                Console.WriteLine( "\n\"{0}\" \nTypeId: {1}",
                    arrayUsageAttr1.ToString(), arrayUsageAttr1.TypeId );
                Console.WriteLine( "\n\"{0}\" \nTypeId: {1}",
                    arrayUsageAttr2.ToString(), arrayUsageAttr2.TypeId );
                Console.WriteLine( "\n\"{0}\" \nTypeId: {1}",
                    listUsageAttr.ToString(), listUsageAttr.TypeId );
            }
            else
                Console.WriteLine( "The parameters information could " +
                    "not be retrieved for method {0}.", mInfo.Name );
        }

        static void Main( )
        {
            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( );
        }
    }
}

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

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_CS.ArgumentUsageAttribute:Must pass an array here."
TypeId: d03a23f4-2536-4478-920f-8b0426dec7f1

"NDP_UE_CS.ArgumentUsageAttribute:Must pass an array here."
TypeId: a1b412e8-3047-49fa-8d03-7660d37ef718

"NDP_UE_CS.ArgumentUsageAttribute:Can pass a param list or array here."
TypeId: 7ac2bf61-0327-48d6-a07e-eb9aaf3dd45e
*/
module NDP_UE_FS

open System

// Define a custom parameter attribute that takes a single message argument.
[<AttributeUsage(AttributeTargets.Parameter)>]
type ArgumentUsageAttribute(usageMsg) =
    inherit Attribute()
    
    let instanceGUID = Guid.NewGuid()
    
    // This is the Message property for the attribute.
    member val Message = usageMsg with get, set

    // Override TypeId to provide a unique identifier for the instance.
    override _.TypeId with get() =
        box instanceGUID

    // Override ToString() to append the message to what the base generates.
    override _.ToString() =
        base.ToString() + ":" + usageMsg

type TestClass() =
    // Assign an ArgumentUsage attribute to each parameter.
    // Assign a ParamArray attribute to strList using the params keyword.
    member _.TestMethod(
        [<ArgumentUsage "Must pass an array here.">]
        strArray: string [],
        [<ArgumentUsage "Can pass a param list or array here."; ParamArray>]
        strList: string []) = ()

let showAttributeTypeIds () =
    // Get the class type, and then get the MethodInfo object
    // for TestMethod to access its metadata.
    let clsType = typeof<TestClass>
    let mInfo = clsType.GetMethod "TestMethod"

    // There will be two elements in pInfoArray, one for each parameter.
    let pInfoArray = mInfo.GetParameters()
    if pInfoArray <> null then
        // Create an instance of the param array attribute on strList.
        let listArrayAttr =
            Attribute.GetCustomAttribute(pInfoArray[1], typeof<ParamArrayAttribute>)
            :?> ParamArrayAttribute

        // Create an instance of the argument usage attribute on strArray.
        let arrayUsageAttr1 =
            Attribute.GetCustomAttribute(pInfoArray[0], typeof<ArgumentUsageAttribute>)
            :?> ArgumentUsageAttribute

        // Create another instance of the argument usage attribute
        // on strArray.
        let arrayUsageAttr2 =
            Attribute.GetCustomAttribute(pInfoArray[0], typeof<ArgumentUsageAttribute>)
            :?> ArgumentUsageAttribute

        // Create an instance of the argument usage attribute on strList.
        let listUsageAttr =
            Attribute.GetCustomAttribute(pInfoArray[1], typeof<ArgumentUsageAttribute>)
            :?> ArgumentUsageAttribute

        // Display the attributes and corresponding TypeId values.
        printfn $"\n\"{listArrayAttr}\" \nTypeId: {listArrayAttr.TypeId}"

        printfn $"\n\"{arrayUsageAttr1}\" \nTypeId: {arrayUsageAttr1.TypeId}"
        printfn $"\n\"{arrayUsageAttr2}\" \nTypeId: {arrayUsageAttr2.TypeId}"
        printfn $"\n\"{listUsageAttr}\" \nTypeId: {listUsageAttr.TypeId}"
    else
        printfn $"The parameters information could not be retrieved for method {mInfo.Name}."

printfn "This example of the Attribute.TypeId property\ngenerates the following output."
printfn "\nCreate instances from a derived Attribute class that implements TypeId, \nand then display the attributes and corresponding TypeId values:"

showAttributeTypeIds ()

// This example of the Attribute.TypeId property
// generates output similar to the following:
//
// 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_FS+ArgumentUsageAttribute:Must pass an array here."
// TypeId: d03a23f4-2536-4478-920f-8b0426dec7f1
//
// "NDP_UE_FS+ArgumentUsageAttribute:Must pass an array here."
// TypeId: a1b412e8-3047-49fa-8d03-7660d37ef718
//
// "NDP_UE_FS+ArgumentUsageAttribute:Can pass a param list or array here."
// TypeId: 7ac2bf61-0327-48d6-a07e-eb9aaf3dd45e
' Example for the Attribute.TypeId property.
Imports System.Reflection

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

        ' 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

    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
    End Class

    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

        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

    End Module ' AttributeTypeIdDemo
End Namespace ' NDP_UE_VB

' This example of the Attribute.TypeId property
' generates output similar to the following:
' 
' 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

Remarques

Tel qu’implémenté, cet identificateur est simplement le Type de l’attribut. Toutefois, il est prévu que l’identificateur unique soit utilisé pour identifier deux attributs du même type.

S’applique à