Attribute.IsDefined Metoda

Definice

Určuje, zda jsou u sestavení, modulu, člena typu nebo parametru metody použity jakékoli vlastní atributy zadaného typu.

Přetížení

IsDefined(ParameterInfo, Type, Boolean)

Určuje, zda jsou na parametr metody použity nějaké vlastní atributy. Parametry určují parametr metody, typ vlastního atributu, který se má hledat, a zda se mají hledat předky parametru metody.

IsDefined(Module, Type, Boolean)

Určuje, zda jsou na modul použity nějaké vlastní atributy. Parametry určují modul, typ vlastního atributu, který chcete vyhledat, a ignorovanou možnost hledání.

IsDefined(MemberInfo, Type, Boolean)

Určuje, zda jsou na člena typu použity nějaké vlastní atributy. Parametry určují člena, typ vlastního atributu, který chcete vyhledat, a zda se mají vyhledávat nadřazené položky členu.

IsDefined(Assembly, Type, Boolean)

Určuje, zda jsou na sestavení použity nějaké vlastní atributy. Parametry určují sestavení, typ vlastního atributu, který chcete vyhledat, a ignorovanou možnost hledání.

IsDefined(MemberInfo, Type)

Určuje, zda jsou na člena typu použity nějaké vlastní atributy. Parametry určují člena a typ vlastního atributu, který chcete vyhledat.

IsDefined(Module, Type)

Určuje, zda jsou na modul použity jakékoli vlastní atributy zadaného typu. Parametry určují modul a typ vlastního atributu, který chcete vyhledat.

IsDefined(Assembly, Type)

Určuje, zda jsou na sestavení použity nějaké vlastní atributy. Parametry určují sestavení a typ vlastního atributu, který chcete vyhledat.

IsDefined(ParameterInfo, Type)

Určuje, zda jsou na parametr metody použity nějaké vlastní atributy. Parametry určují parametr metody a typ vlastního atributu, který chcete vyhledat.

IsDefined(ParameterInfo, Type, Boolean)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

Určuje, zda jsou na parametr metody použity nějaké vlastní atributy. Parametry určují parametr metody, typ vlastního atributu, který se má hledat, a zda se mají hledat předky parametru metody.

public:
 static bool IsDefined(System::Reflection::ParameterInfo ^ element, Type ^ attributeType, bool inherit);
public static bool IsDefined (System.Reflection.ParameterInfo element, Type attributeType, bool inherit);
static member IsDefined : System.Reflection.ParameterInfo * Type * bool -> bool
Public Shared Function IsDefined (element As ParameterInfo, attributeType As Type, inherit As Boolean) As Boolean

Parametry

element
ParameterInfo

Objekt odvozený z ParameterInfo třídy, která popisuje parametr člena třídy.

attributeType
Type

Typ nebo základní typ vlastního atributu, který chcete vyhledat.

inherit
Boolean

Pokud true, určuje, že se vlastní atributy prohledávají také v nadřazených element atributech .

Návraty

truepokud se vlastní atribut typu attributeType použije na element, jinak . false

Výjimky

element nebo attributeType je null.

attributeType není odvozená z Attribute.

element není metoda, konstruktor ani typ.

Příklady

Následující příklad kódu ukazuje použití , IsDefinedkterý jako parametr přebírá .ParameterInfo

using namespace System;
using namespace System::Reflection;

namespace IsDef5CS
{
   public ref class TestClass
   {
   public:

      // Assign a ParamArray attribute to the parameter using the keyword.
      void Method1(... array<String^>^args ){}

   };

   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = TestClass::typeid;
         
         // Get the MethodInfo object for Method1.
         MethodInfo^ mInfo = clsType->GetMethod( "Method1" );
         
         // Get the ParameterInfo array for the method parameters.
         array<ParameterInfo^>^pInfo = mInfo->GetParameters();
         if ( pInfo != nullptr )
         {
            
            // See if the ParamArray attribute is defined.
            bool isDef = Attribute::IsDefined( pInfo[ 0 ], ParamArrayAttribute::typeid );
            
            // Display the result.
            Console::WriteLine( "The ParamArray attribute {0} defined for "
            "parameter {1} of method {2}.", isDef ? (String^)"is" : "is not", pInfo[ 0 ]->Name, mInfo->Name );
         }
         else
                  Console::WriteLine( "The parameters information could "
         "not be retrieved for method {0}.", mInfo->Name );
      }

   };

}

/*
 * Output:
 * The ParamArray attribute is defined for parameter args of method Method1.
 */
using System;
using System.Reflection;

namespace IsDef5CS
{
    public class TestClass
    {
        // Assign a ParamArray attribute to the parameter using the keyword.
        public void Method1(params String[] args)
        {}
    }

    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(TestClass);
            // Get the MethodInfo object for Method1.
            MethodInfo mInfo = clsType.GetMethod("Method1");
            // Get the ParameterInfo array for the method parameters.
            ParameterInfo[] pInfo = mInfo.GetParameters();
            if (pInfo != null)
            {
                // See if the ParamArray attribute is defined.
                bool isDef = Attribute.IsDefined(pInfo[0],
                                                 typeof(ParamArrayAttribute));
                // Display the result.
                Console.WriteLine("The ParamArray attribute {0} defined for " +
                                  "parameter {1} of method {2}.",
                                  isDef ? "is" : "is not",
                                  pInfo[0].Name,
                                  mInfo.Name);
            }
            else
                Console.WriteLine("The parameters information could " +
                            "not be retrieved for method {0}.", mInfo.Name);
        }
    }
}

/*
 * Output:
 * The ParamArray attribute is defined for parameter args of method Method1.
 */
open System;

type TestClass() =
    // Assign a ParamArray attribute to the parameter.
    member _.Method1([<ParamArray>] args: string[]) = ()

// Get the class type to access its metadata.
let clsType = typeof<TestClass>

// Get the MethodInfo object for Method1.
let mInfo = clsType.GetMethod "Method1"

// Get the ParameterInfo array for the method parameters.
let pInfo = mInfo.GetParameters()

if pInfo <> null then
    // See if the ParamArray attribute is defined.
    let isDef = Attribute.IsDefined(pInfo[0], typeof<ParamArrayAttribute>)

    // Display the result.
    printfn $"""The ParamArray attribute {if isDef then "is" else "is not"} defined for parameter {pInfo[0].Name} of method {mInfo.Name}."""
else
    printfn $"The parameters information could not be retrieved for method {mInfo.Name}."

// Output:
//  The ParamArray attribute is defined for parameter args of method Method1.
Imports System.Reflection

Module DemoModule
    Public Class TestClass
        ' Assign a ParamArray attribute to the parameter using the keyword.
        Public Sub Method1(ByVal ParamArray args As String())
        End Sub
    End Class

    Sub Main()
        ' Get the class type to access its metadata.
        Dim clsType As Type = GetType(TestClass)
        ' Get the MethodInfo object for Method1.
        Dim mInfo As MethodInfo = clsType.GetMethod("Method1")
        ' Get the ParameterInfo array for the method parameters.
        Dim pInfo() As ParameterInfo = mInfo.GetParameters()
        If Not pInfo(0) Is Nothing Then
            ' See if the ParamArray attribute is defined.
            Dim isDef As Boolean = Attribute.IsDefined(pInfo(0), _
                                   GetType(ParamArrayAttribute))
            Dim strDef As String
            If isDef = True Then
                strDef = "is"
            Else
                strDef = "is not"
            End If
            ' Display the result.
            Console.WriteLine("The ParamArray attribute {0} defined " & _
                              "for parameter {1} of method {2}.", _
                              strDef, pInfo(0).Name, mInfo.Name)
        Else
            Console.WriteLine("Could not retrieve parameter information " & _
                              "for method {0}.", mInfo.Name)
        End If
    End Sub
End Module

' Output:
' The ParamArray attribute is defined for parameter args of method Method1.

Platí pro

IsDefined(Module, Type, Boolean)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

Určuje, zda jsou na modul použity nějaké vlastní atributy. Parametry určují modul, typ vlastního atributu, který chcete vyhledat, a ignorovanou možnost hledání.

public:
 static bool IsDefined(System::Reflection::Module ^ element, Type ^ attributeType, bool inherit);
public static bool IsDefined (System.Reflection.Module element, Type attributeType, bool inherit);
static member IsDefined : System.Reflection.Module * Type * bool -> bool
Public Shared Function IsDefined (element As Module, attributeType As Type, inherit As Boolean) As Boolean

Parametry

element
Module

Objekt odvozený z Module třídy, která popisuje přenosný spustitelný soubor.

attributeType
Type

Typ nebo základní typ vlastního atributu, který chcete vyhledat.

inherit
Boolean

Tento parametr je ignorován a nemá vliv na provoz této metody.

Návraty

truepokud se vlastní atribut typu attributeType použije na element, jinak . false

Výjimky

element nebo attributeType je null.

attributeType není odvozená z Attribute.

Příklady

Následující příklad kódu ukazuje použití , IsDefinedkterý jako parametr přebírá .Module

using namespace System;
using namespace System::Diagnostics;

// Add the Debuggable attribute to the module.
[module:Debuggable(true,false)];
namespace IsDef2CS
{
   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = DemoClass::typeid;
         
         // See if the Debuggable attribute is defined for this module.
         bool isDef = Attribute::IsDefined( clsType->Module, DebuggableAttribute::typeid );
         
         // Display the result.
         Console::WriteLine( "The Debuggable attribute {0} "
         "defined for Module {1}.", isDef ? (String^)"is" : "is not", clsType->Module->Name );
         
         // If the attribute is defined, display the JIT settings.
         if ( isDef )
         {
            
            // Retrieve the attribute itself.
            DebuggableAttribute^ dbgAttr = dynamic_cast<DebuggableAttribute^>(Attribute::GetCustomAttribute( clsType->Module, DebuggableAttribute::typeid ));
            if ( dbgAttr != nullptr )
            {
               Console::WriteLine( "JITTrackingEnabled is {0}.", dbgAttr->IsJITTrackingEnabled );
               Console::WriteLine( "JITOptimizerDisabled is {0}.", dbgAttr->IsJITOptimizerDisabled );
            }
            else
                        Console::WriteLine( "The Debuggable attribute "
            "could not be retrieved." );
         }
      }

   };

}


/*
 * Output:
 * The Debuggable attribute is defined for Module IsDef2CS.exe.
 * JITTrackingEnabled is True.
 * JITOptimizerDisabled is False.
 */
using System;
using System.Diagnostics;

// Add the Debuggable attribute to the module.
[module:Debuggable(true, false)]
namespace IsDef2CS
{
    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(DemoClass);
            // See if the Debuggable attribute is defined for this module.
            bool isDef = Attribute.IsDefined(clsType.Module,
                typeof(DebuggableAttribute));
            // Display the result.
            Console.WriteLine("The Debuggable attribute {0} " +
                "defined for Module {1}.",
                isDef ? "is" : "is not",
                clsType.Module.Name);
            // If the attribute is defined, display the JIT settings.
            if (isDef)
            {
                // Retrieve the attribute itself.
                DebuggableAttribute dbgAttr = (DebuggableAttribute)
                    Attribute.GetCustomAttribute(clsType.Module,
                    typeof(DebuggableAttribute));
                if (dbgAttr != null)
                {
                    Console.WriteLine("JITTrackingEnabled is {0}.",
                        dbgAttr.IsJITTrackingEnabled);
                    Console.WriteLine("JITOptimizerDisabled is {0}.",
                        dbgAttr.IsJITOptimizerDisabled);
                }
                else
                    Console.WriteLine("The Debuggable attribute " +
                        "could not be retrieved.");
            }
        }
    }
}

/*
 * Output:
 * The Debuggable attribute is defined for Module IsDef2CS.exe.
 * JITTrackingEnabled is True.
 * JITOptimizerDisabled is False.
 */
open System
open System.Diagnostics

// Add the Debuggable attribute to the module.
[<``module``: Debuggable(true, false)>]
do ()
    
type DemoClass = class end

// Get the class type to access its metadata.
let clsType = typeof<DemoClass>

// See if the Debuggable attribute is defined for this module.
let isDef = Attribute.IsDefined(clsType.Module, typeof<DebuggableAttribute>)

// Display the result.
printfn $"""The Debuggable attribute {if isDef then "is" else "is not"} defined for Module {clsType.Module.Name}."""

// If the attribute is defined, display the JIT settings.
if isDef then
    // Retrieve the attribute itself.
    let dbgAttr =
        Attribute.GetCustomAttribute(clsType.Module, typeof<DebuggableAttribute>)
        :?> DebuggableAttribute

    if dbgAttr <> null then
        printfn $"JITTrackingEnabled is {dbgAttr.IsJITTrackingEnabled}."
        printfn $"JITOptimizerDisabled is {dbgAttr.IsJITOptimizerDisabled}."            
    else
        printfn "The Debuggable attribute could not be retrieved."

// Output:
//  The Debuggable attribute is defined for Module IsDef2CS.exe.
//  JITTrackingEnabled is True.
//  JITOptimizerDisabled is False.
Imports System.Reflection
Imports System.Diagnostics

' Add the Debuggable attribute to the module.
<Module: Debuggable(True, False)> 

Module DemoModule
    Sub Main()
        ' Get the module type information to access its metadata.
        Dim modType As Type = GetType(DemoModule)
        ' See if the Debuggable attribute is defined.
        Dim isDef As Boolean = Attribute.IsDefined(modType.Module, _
                               GetType(DebuggableAttribute))
        Dim strDef As String
        If isDef = True Then
            strDef = "is"
        Else
            strDef = "is not"
        End If
        ' Display the result
        Console.WriteLine("The debuggable attribute {0} defined for " & _
                          "module {1}.", strDef, modType.Name)
        ' If the attribute is defined, display the JIT settings.
        If isDef = True Then
            ' Retrieve the attribute itself.
            Dim attr As Attribute = _
                Attribute.GetCustomAttribute(modType.Module, _
                GetType(DebuggableAttribute))
            If Not attr Is Nothing And TypeOf attr Is DebuggableAttribute Then
                Dim dbgAttr As DebuggableAttribute = _
                    CType(attr, DebuggableAttribute)
                Console.WriteLine("JITTrackingEnabled is {0}.", _
                    dbgAttr.IsJITTrackingEnabled.ToString())
                Console.WriteLine("JITOptimizerDisabled is {0}.", _
                    dbgAttr.IsJITOptimizerDisabled.ToString())
            Else
                Console.WriteLine("The Debuggable attribute could " & _
                                  "not be retrieved.")
            End If
        End If
    End Sub
End Module

' Output:
' The debuggable attribute is defined for module DemoModule.
' JITTrackingEnabled is True.
' JITOptimizerDisabled is False.

Poznámky

Tato metoda ignoruje inherit parametr a nevyhledává vlastní atributy v nadřazených element atributech .

Platí pro

IsDefined(MemberInfo, Type, Boolean)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

Určuje, zda jsou na člena typu použity nějaké vlastní atributy. Parametry určují člena, typ vlastního atributu, který chcete vyhledat, a zda se mají vyhledávat nadřazené položky členu.

public:
 static bool IsDefined(System::Reflection::MemberInfo ^ element, Type ^ attributeType, bool inherit);
public static bool IsDefined (System.Reflection.MemberInfo element, Type attributeType, bool inherit);
static member IsDefined : System.Reflection.MemberInfo * Type * bool -> bool
Public Shared Function IsDefined (element As MemberInfo, attributeType As Type, inherit As Boolean) As Boolean

Parametry

element
MemberInfo

Objekt odvozený z MemberInfo třídy, která popisuje konstruktor, událost, pole, metodu, typ nebo vlastnost člen třídy.

attributeType
Type

Typ nebo základní typ vlastního atributu, který chcete vyhledat.

inherit
Boolean

Pokud true, určuje, že se vlastní atributy prohledávají také v nadřazených element atributech .

Návraty

truepokud se vlastní atribut typu attributeType použije na element, jinak . false

Výjimky

element nebo attributeType je null.

attributeType není odvozená z Attribute.

element není konstruktor, metoda, vlastnost, událost, typ nebo pole.

Příklady

Následující příklad kódu ukazuje použití , IsDefinedkterý jako parametr přebírá .MemberInfo

using namespace System;
using namespace System::Reflection;

namespace IsDef4CS
{
   public ref class TestClass
   {
   public:

      // Assign the Obsolete attribute to a method.

      [Obsolete("This method is obsolete. Use Method2 instead.")]
      void Method1(){}

      void Method2(){}

   };

   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = TestClass::typeid;
         
         // Get the MethodInfo object for Method1.
         MethodInfo^ mInfo = clsType->GetMethod( "Method1" );
         
         // See if the Obsolete attribute is defined for this method.
         bool isDef = Attribute::IsDefined( mInfo, ObsoleteAttribute::typeid );
         
         // Display the result.
         Console::WriteLine( "The Obsolete Attribute {0} defined for {1} of class {2}.", isDef ? (String^)"is" : "is not", mInfo->Name, clsType->Name );
         
         // If it's defined, display the attribute's message.
         if ( isDef )
         {
            ObsoleteAttribute^ obsAttr = dynamic_cast<ObsoleteAttribute^>(Attribute::GetCustomAttribute( mInfo, ObsoleteAttribute::typeid ));
            if ( obsAttr != nullptr )
                        Console::WriteLine( "The message is: \"{0}\".", obsAttr->Message );
            else
                        Console::WriteLine( "The message could not be retrieved." );
         }
      }

   };

}


/*
 * Output:
 * The Obsolete Attribute is defined for Method1 of class TestClass.
 * The message is: "This method is obsolete. Use Method2 instead.".
 */
using System;
using System.Reflection;

namespace IsDef4CS
{
    public class TestClass
    {
        // Assign the Obsolete attribute to a method.
        [Obsolete("This method is obsolete. Use Method2 instead.")]
        public void Method1()
        {}
        public void Method2()
        {}
    }

    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(TestClass);
            // Get the MethodInfo object for Method1.
            MethodInfo mInfo = clsType.GetMethod("Method1");
            // See if the Obsolete attribute is defined for this method.
            bool isDef = Attribute.IsDefined(mInfo, typeof(ObsoleteAttribute));
            // Display the result.
            Console.WriteLine("The Obsolete Attribute {0} defined for {1} of class {2}.",
                isDef ? "is" : "is not", mInfo.Name, clsType.Name);
            // If it's defined, display the attribute's message.
            if (isDef)
            {
                ObsoleteAttribute obsAttr =
                                 (ObsoleteAttribute)Attribute.GetCustomAttribute(
                                                    mInfo, typeof(ObsoleteAttribute));
                if (obsAttr != null)
                    Console.WriteLine("The message is: \"{0}\".",
                        obsAttr.Message);
                else
                    Console.WriteLine("The message could not be retrieved.");
            }
        }
    }
}

/*
 * Output:
 * The Obsolete Attribute is defined for Method1 of class TestClass.
 * The message is: "This method is obsolete. Use Method2 instead.".
 */
open System

type TestClass() =
    // Assign the Obsolete attribute to a method.
    [<Obsolete "This method is obsolete. Use Method2 instead.">]
    member _.Method1() = ()
    member _.Method2() = ()

// Get the class type to access its metadata.
let clsType = typeof<TestClass>

// Get the MethodInfo object for Method1.
let mInfo = clsType.GetMethod "Method1"

// See if the Obsolete attribute is defined for this method.
let isDef = Attribute.IsDefined(mInfo, typeof<ObsoleteAttribute>)

// Display the result.
printfn $"""The Obsolete Attribute {if isDef then "is" else "is not"} defined for {mInfo.Name} of class {clsType.Name}."""

// If it's defined, display the attribute's message.
if isDef then
    let obsAttr =
        Attribute.GetCustomAttribute(mInfo, typeof<ObsoleteAttribute>)
        :?> ObsoleteAttribute
    if obsAttr <> null then
        printfn $"The message is: \"{obsAttr.Message}\"."
    else
        printfn "The message could not be retrieved."

// Output:
//  The Obsolete Attribute is defined for Method1 of class TestClass.
// The message is: "This method is obsolete. Use Method2 instead.".
Imports System.Reflection

Module DemoModule

    Public Class TestClass
        ' Assign the Obsolete attribute to a method.
        <Obsolete("This method is obsolete. Use Method2() instead.")> _
        Public Sub Method1()
        End Sub

        Public Sub Method2()
        End Sub
    End Class

    Sub Main()
        ' Get the class type to access its metadata.
        Dim clsType As Type = GetType(TestClass)
        ' Get the MethodInfo object for Method1.
        Dim mInfo As MethodInfo = clsType.GetMethod("Method1")
        ' See if the Obsolete attribute is defined for this method.
        Dim isDef As Boolean = Attribute.IsDefined(mInfo, _
            GetType(ObsoleteAttribute))
        Dim strDef As String
        If isDef = True Then
            strDef = "is"
        Else
            strDef = "is not"
        End If
        ' Display the results.
        Console.WriteLine("The Obsolete attribute {0} defined for " & _
            "method {1} of class {2}.", strDef, mInfo.Name, clsType.Name)
        ' If it's defined, display the attribute's message.
        If isDef = True Then
            Dim attr As Attribute = Attribute.GetCustomAttribute(mInfo, _
                GetType(ObsoleteAttribute))
            If Not attr Is Nothing And TypeOf attr Is ObsoleteAttribute Then
                Dim obsAttr As ObsoleteAttribute = _
                    CType(attr, ObsoleteAttribute)
                Console.WriteLine("The message is: ""{0}""", obsAttr.Message)
            Else
                Console.WriteLine("The message could not be retrieved.")
            End If
        End If
    End Sub
End Module

' Output:
' The Obsolete attribute is defined for method Method1 of class TestClass.
' The message is: "This method is obsolete. Use Method2() instead."

Poznámky

Poznámka

Počínaje rozhraním .NET Framework verze 2.0 tato metoda vrátí true , pokud typ, metoda nebo konstruktor má atributy zabezpečení uložené v novém formátu metadat. Sestavení kompilovaná s verzí 2.0 nebo novější používají nový formát. Dynamická sestavení a sestavení zkompilovaná s dřívějšími verzemi rozhraní .NET Framework používají starý formát XML. Viz Generování deklarativních atributů zabezpečení.

Platí pro

IsDefined(Assembly, Type, Boolean)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

Určuje, zda jsou na sestavení použity nějaké vlastní atributy. Parametry určují sestavení, typ vlastního atributu, který chcete vyhledat, a ignorovanou možnost hledání.

public:
 static bool IsDefined(System::Reflection::Assembly ^ element, Type ^ attributeType, bool inherit);
public static bool IsDefined (System.Reflection.Assembly element, Type attributeType, bool inherit);
static member IsDefined : System.Reflection.Assembly * Type * bool -> bool
Public Shared Function IsDefined (element As Assembly, attributeType As Type, inherit As Boolean) As Boolean

Parametry

element
Assembly

Objekt odvozený z Assembly třídy, která popisuje opakovaně použitelnou kolekci modulů.

attributeType
Type

Typ nebo základní typ vlastního atributu, který chcete vyhledat.

inherit
Boolean

Tento parametr je ignorován a nemá vliv na provoz této metody.

Návraty

truepokud se vlastní atribut typu attributeType použije na element, jinak . false

Výjimky

element nebo attributeType je null.

attributeType není odvozená z Attribute.

Příklady

Následující příklad kódu ukazuje použití , IsDefinedkterý jako parametr přebírá Assembly .

using namespace System;
using namespace System::Reflection;

// Add an AssemblyDescription attribute
[assembly:AssemblyDescription("A sample description")];
namespace IsDef1CS
{
   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = DemoClass::typeid;
         
         // Get the assembly object.
         Assembly^ assy = clsType->Assembly;
         
         // Store the assembly's name.
         String^ assyName = assy->GetName()->Name;
         
         //Type assyType = assy.GetType();
         // See if the Assembly Description is defined.
         bool isdef = Attribute::IsDefined( assy, AssemblyDescriptionAttribute::typeid );
         if ( isdef )
         {
            
            // Affirm that the attribute is defined.
            Console::WriteLine( "The AssemblyDescription attribute "
            "is defined for assembly {0}.", assyName );
            
            // Get the description attribute itself.
            AssemblyDescriptionAttribute^ adAttr = dynamic_cast<AssemblyDescriptionAttribute^>(Attribute::GetCustomAttribute( assy, AssemblyDescriptionAttribute::typeid ));
            
            // Display the description.
            if ( adAttr != nullptr )
                        Console::WriteLine( "The description is \"{0}\".", adAttr->Description );
            else
                        Console::WriteLine( "The description could not "
            "be retrieved." );
         }
         else
                  Console::WriteLine( "The AssemblyDescription attribute is not "
         "defined for assembly {0}.", assyName );
      }

   };

}


/*
 * Output:
 * The AssemblyDescription attributeis defined for assembly IsDef1CS.
 * The description is "A sample description".
 */
using System;
using System.Reflection;

// Add an AssemblyDescription attribute
[assembly: AssemblyDescription("A sample description")]
namespace IsDef1CS
{
    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(DemoClass);
            // Get the assembly object.
            Assembly assy = clsType.Assembly;
            // Store the assembly's name.
            String assyName = assy.GetName().Name;
            // See if the Assembly Description is defined.
            bool isdef = Attribute.IsDefined(assy,
                typeof(AssemblyDescriptionAttribute));
            if (isdef)
            {
                // Affirm that the attribute is defined.
                Console.WriteLine("The AssemblyDescription attribute " +
                    "is defined for assembly {0}.", assyName);
                // Get the description attribute itself.
                AssemblyDescriptionAttribute adAttr =
                    (AssemblyDescriptionAttribute)Attribute.GetCustomAttribute(
                    assy, typeof(AssemblyDescriptionAttribute));
                // Display the description.
                if (adAttr != null)
                    Console.WriteLine("The description is \"{0}\".",
                        adAttr.Description);
                else
                    Console.WriteLine("The description could not " +
                        "be retrieved.");
            }
            else
                Console.WriteLine("The AssemblyDescription attribute is not " +
                    "defined for assembly {0}.", assyName);
        }
    }
}

/*
 * Output:
 * The AssemblyDescription attribute is defined for assembly IsDef1CS.
 * The description is "A sample description".
 */
open System
open System.Reflection

// Add an AssemblyDescription attribute
[<assembly: AssemblyDescription "A sample description">]
do ()

type DemoClass = class end

// Get the class type to access its metadata.
let clsType = typeof<DemoClass>

// Get the assembly object.
let assembly = clsType.Assembly;

// Store the assembly's name.
let assemblyName = assembly.GetName().Name

// See if the Assembly Description is defined.
let isdef = 
    Attribute.IsDefined(assembly, typeof<AssemblyDescriptionAttribute>)

if isdef then
    // Affirm that the attribute is defined.
    printfn $"The AssemblyDescription attribute is defined for assembly {assemblyName}."
    
    // Get the description attribute itself.
    let adAttr =
        Attribute.GetCustomAttribute(assembly, typeof<AssemblyDescriptionAttribute>)
        :?> AssemblyDescriptionAttribute

    // Display the description.
    if adAttr <> null then
        printfn $"The description is \"{adAttr.Description}\"."
    else
        printfn $"The description could not be retrieved."
else
    printfn $"The AssemblyDescription attribute is not defined for assembly {assemblyName}."

// Output:
//  The AssemblyDescription attribute is defined for assembly IsDef1FS.
//  The description is "A sample description".
Imports System.Reflection

' Add an AssemblyDescription attribute.
<Assembly: AssemblyDescription("A sample description")> 

Module DemoModule
    Sub Main()
        ' Get the assembly for this module.
        Dim assy As System.Reflection.Assembly = GetType(DemoModule).Assembly
        ' Store the assembly name.
        Dim assyName As String = assy.GetName().Name
        ' See if the AssemblyDescription attribute is defined.
        If Attribute.IsDefined(assy, GetType(AssemblyDescriptionAttribute)) _
            Then
            ' Affirm that the attribute is defined. Assume the filename of
            ' this code example is "IsDef1VB".
            Console.WriteLine("The AssemblyDescription attribute is " & _
                "defined for assembly {0}.", assyName)
            ' Get the description attribute itself.
            Dim attr As Attribute = Attribute.GetCustomAttribute( _
                assy, GetType(AssemblyDescriptionAttribute))
            ' Display the description.
            If Not attr Is Nothing And _
                TypeOf attr Is AssemblyDescriptionAttribute Then
                Dim adAttr As AssemblyDescriptionAttribute = _
                    CType(attr, AssemblyDescriptionAttribute)
                Console.WriteLine("The description is " & _
                    Chr(34) & "{0}" & Chr(34) & ".", adAttr.Description)
            Else
                Console.WriteLine("The description could not be retrieved.")
            End If
        Else
            Console.WriteLine("The AssemblyDescription attribute is not " & _
                              "defined for assembly {0}.", assyName)
        End If
    End Sub
End Module

' Output:
' The AssemblyDescription attribute is defined for assembly IsDef1VB.
' The description is "A sample description".

Poznámky

Poznámka

Počínaje rozhraním .NET Framework verze 2.0 tato metoda vrátí true , pokud sestavení má atributy zabezpečení uložené v novém formátu metadat. Sestavení kompilovaná s verzí 2.0 nebo novější používají nový formát. Dynamická sestavení a sestavení zkompilovaná s dřívějšími verzemi rozhraní .NET Framework používají starý formát XML. Viz Generování deklarativních atributů zabezpečení.

Platí pro

IsDefined(MemberInfo, Type)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

Určuje, zda jsou na člena typu použity nějaké vlastní atributy. Parametry určují člena a typ vlastního atributu, který chcete vyhledat.

public:
 static bool IsDefined(System::Reflection::MemberInfo ^ element, Type ^ attributeType);
public static bool IsDefined (System.Reflection.MemberInfo element, Type attributeType);
static member IsDefined : System.Reflection.MemberInfo * Type -> bool
Public Shared Function IsDefined (element As MemberInfo, attributeType As Type) As Boolean

Parametry

element
MemberInfo

Objekt odvozený z MemberInfo třídy, která popisuje konstruktor, událost, pole, metodu, typ nebo vlastnost člen třídy.

attributeType
Type

Typ nebo základní typ vlastního atributu, který chcete vyhledat.

Návraty

truepokud se vlastní atribut typu attributeType použije na element, jinak . false

Výjimky

element nebo attributeType je null.

attributeType není odvozená z Attribute.

element není konstruktor, metoda, vlastnost, událost, typ nebo pole.

Příklady

Následující příklad kódu ukazuje použití , IsDefinedkterý jako parametr přebírá .MemberInfo

using namespace System;
using namespace System::Reflection;

namespace IsDef4CS
{
   public ref class TestClass
   {
   public:

      // Assign the Obsolete attribute to a method.

      [Obsolete("This method is obsolete. Use Method2 instead.")]
      void Method1(){}

      void Method2(){}

   };

   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = TestClass::typeid;
         
         // Get the MethodInfo object for Method1.
         MethodInfo^ mInfo = clsType->GetMethod( "Method1" );
         
         // See if the Obsolete attribute is defined for this method.
         bool isDef = Attribute::IsDefined( mInfo, ObsoleteAttribute::typeid );
         
         // Display the result.
         Console::WriteLine( "The Obsolete Attribute {0} defined for {1} of class {2}.", isDef ? (String^)"is" : "is not", mInfo->Name, clsType->Name );
         
         // If it's defined, display the attribute's message.
         if ( isDef )
         {
            ObsoleteAttribute^ obsAttr = dynamic_cast<ObsoleteAttribute^>(Attribute::GetCustomAttribute( mInfo, ObsoleteAttribute::typeid ));
            if ( obsAttr != nullptr )
                        Console::WriteLine( "The message is: \"{0}\".", obsAttr->Message );
            else
                        Console::WriteLine( "The message could not be retrieved." );
         }
      }

   };

}


/*
 * Output:
 * The Obsolete Attribute is defined for Method1 of class TestClass.
 * The message is: "This method is obsolete. Use Method2 instead.".
 */
using System;
using System.Reflection;

namespace IsDef4CS
{
    public class TestClass
    {
        // Assign the Obsolete attribute to a method.
        [Obsolete("This method is obsolete. Use Method2 instead.")]
        public void Method1()
        {}
        public void Method2()
        {}
    }

    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(TestClass);
            // Get the MethodInfo object for Method1.
            MethodInfo mInfo = clsType.GetMethod("Method1");
            // See if the Obsolete attribute is defined for this method.
            bool isDef = Attribute.IsDefined(mInfo, typeof(ObsoleteAttribute));
            // Display the result.
            Console.WriteLine("The Obsolete Attribute {0} defined for {1} of class {2}.",
                isDef ? "is" : "is not", mInfo.Name, clsType.Name);
            // If it's defined, display the attribute's message.
            if (isDef)
            {
                ObsoleteAttribute obsAttr =
                                 (ObsoleteAttribute)Attribute.GetCustomAttribute(
                                                    mInfo, typeof(ObsoleteAttribute));
                if (obsAttr != null)
                    Console.WriteLine("The message is: \"{0}\".",
                        obsAttr.Message);
                else
                    Console.WriteLine("The message could not be retrieved.");
            }
        }
    }
}

/*
 * Output:
 * The Obsolete Attribute is defined for Method1 of class TestClass.
 * The message is: "This method is obsolete. Use Method2 instead.".
 */
open System

type TestClass() =
    // Assign the Obsolete attribute to a method.
    [<Obsolete "This method is obsolete. Use Method2 instead.">]
    member _.Method1() = ()
    member _.Method2() = ()

// Get the class type to access its metadata.
let clsType = typeof<TestClass>

// Get the MethodInfo object for Method1.
let mInfo = clsType.GetMethod "Method1"

// See if the Obsolete attribute is defined for this method.
let isDef = Attribute.IsDefined(mInfo, typeof<ObsoleteAttribute>)

// Display the result.
printfn $"""The Obsolete Attribute {if isDef then "is" else "is not"} defined for {mInfo.Name} of class {clsType.Name}."""

// If it's defined, display the attribute's message.
if isDef then
    let obsAttr =
        Attribute.GetCustomAttribute(mInfo, typeof<ObsoleteAttribute>)
        :?> ObsoleteAttribute
    if obsAttr <> null then
        printfn $"The message is: \"{obsAttr.Message}\"."
    else
        printfn "The message could not be retrieved."

// Output:
//  The Obsolete Attribute is defined for Method1 of class TestClass.
// The message is: "This method is obsolete. Use Method2 instead.".
Imports System.Reflection

Module DemoModule

    Public Class TestClass
        ' Assign the Obsolete attribute to a method.
        <Obsolete("This method is obsolete. Use Method2() instead.")> _
        Public Sub Method1()
        End Sub

        Public Sub Method2()
        End Sub
    End Class

    Sub Main()
        ' Get the class type to access its metadata.
        Dim clsType As Type = GetType(TestClass)
        ' Get the MethodInfo object for Method1.
        Dim mInfo As MethodInfo = clsType.GetMethod("Method1")
        ' See if the Obsolete attribute is defined for this method.
        Dim isDef As Boolean = Attribute.IsDefined(mInfo, _
            GetType(ObsoleteAttribute))
        Dim strDef As String
        If isDef = True Then
            strDef = "is"
        Else
            strDef = "is not"
        End If
        ' Display the results.
        Console.WriteLine("The Obsolete attribute {0} defined for " & _
            "method {1} of class {2}.", strDef, mInfo.Name, clsType.Name)
        ' If it's defined, display the attribute's message.
        If isDef = True Then
            Dim attr As Attribute = Attribute.GetCustomAttribute(mInfo, _
                GetType(ObsoleteAttribute))
            If Not attr Is Nothing And TypeOf attr Is ObsoleteAttribute Then
                Dim obsAttr As ObsoleteAttribute = _
                    CType(attr, ObsoleteAttribute)
                Console.WriteLine("The message is: ""{0}""", obsAttr.Message)
            Else
                Console.WriteLine("The message could not be retrieved.")
            End If
        End If
    End Sub
End Module

' Output:
' The Obsolete attribute is defined for method Method1 of class TestClass.
' The message is: "This method is obsolete. Use Method2() instead."

Poznámky

Vyhledávají se vlastní element atributy.

Poznámka

Počínaje rozhraním .NET Framework verze 2.0 tato metoda vrátí true , pokud typ, metoda nebo konstruktor má atributy zabezpečení uložené v novém formátu metadat. Sestavení kompilovaná s verzí 2.0 nebo novější používají nový formát. Dynamická sestavení a sestavení zkompilovaná s dřívějšími verzemi rozhraní .NET Framework používají starý formát XML. Viz Generování deklarativních atributů zabezpečení.

Platí pro

IsDefined(Module, Type)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

Určuje, zda se na modul použijí nějaké vlastní atributy zadaného typu. Parametry určují modul a typ vlastního atributu, který chcete vyhledat.

public:
 static bool IsDefined(System::Reflection::Module ^ element, Type ^ attributeType);
public static bool IsDefined (System.Reflection.Module element, Type attributeType);
static member IsDefined : System.Reflection.Module * Type -> bool
Public Shared Function IsDefined (element As Module, attributeType As Type) As Boolean

Parametry

element
Module

Objekt odvozený z Module třídy, která popisuje přenosný spustitelný soubor.

attributeType
Type

Typ nebo základní typ vlastního atributu, který chcete vyhledat.

Návraty

truepokud se pro elementpoužije vlastní atribut typu attributeType , v opačném případě false.

Výjimky

element nebo attributeType je null.

attributeType není odvozeno od Attribute.

Příklady

Následující příklad kódu ukazuje použití , IsDefinedkterý jako parametr přebírá .Module

using namespace System;
using namespace System::Diagnostics;

// Add the Debuggable attribute to the module.
[module:Debuggable(true,false)];
namespace IsDef2CS
{
   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = DemoClass::typeid;
         
         // See if the Debuggable attribute is defined for this module.
         bool isDef = Attribute::IsDefined( clsType->Module, DebuggableAttribute::typeid );
         
         // Display the result.
         Console::WriteLine( "The Debuggable attribute {0} "
         "defined for Module {1}.", isDef ? (String^)"is" : "is not", clsType->Module->Name );
         
         // If the attribute is defined, display the JIT settings.
         if ( isDef )
         {
            
            // Retrieve the attribute itself.
            DebuggableAttribute^ dbgAttr = dynamic_cast<DebuggableAttribute^>(Attribute::GetCustomAttribute( clsType->Module, DebuggableAttribute::typeid ));
            if ( dbgAttr != nullptr )
            {
               Console::WriteLine( "JITTrackingEnabled is {0}.", dbgAttr->IsJITTrackingEnabled );
               Console::WriteLine( "JITOptimizerDisabled is {0}.", dbgAttr->IsJITOptimizerDisabled );
            }
            else
                        Console::WriteLine( "The Debuggable attribute "
            "could not be retrieved." );
         }
      }

   };

}


/*
 * Output:
 * The Debuggable attribute is defined for Module IsDef2CS.exe.
 * JITTrackingEnabled is True.
 * JITOptimizerDisabled is False.
 */
using System;
using System.Diagnostics;

// Add the Debuggable attribute to the module.
[module:Debuggable(true, false)]
namespace IsDef2CS
{
    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(DemoClass);
            // See if the Debuggable attribute is defined for this module.
            bool isDef = Attribute.IsDefined(clsType.Module,
                typeof(DebuggableAttribute));
            // Display the result.
            Console.WriteLine("The Debuggable attribute {0} " +
                "defined for Module {1}.",
                isDef ? "is" : "is not",
                clsType.Module.Name);
            // If the attribute is defined, display the JIT settings.
            if (isDef)
            {
                // Retrieve the attribute itself.
                DebuggableAttribute dbgAttr = (DebuggableAttribute)
                    Attribute.GetCustomAttribute(clsType.Module,
                    typeof(DebuggableAttribute));
                if (dbgAttr != null)
                {
                    Console.WriteLine("JITTrackingEnabled is {0}.",
                        dbgAttr.IsJITTrackingEnabled);
                    Console.WriteLine("JITOptimizerDisabled is {0}.",
                        dbgAttr.IsJITOptimizerDisabled);
                }
                else
                    Console.WriteLine("The Debuggable attribute " +
                        "could not be retrieved.");
            }
        }
    }
}

/*
 * Output:
 * The Debuggable attribute is defined for Module IsDef2CS.exe.
 * JITTrackingEnabled is True.
 * JITOptimizerDisabled is False.
 */
open System
open System.Diagnostics

// Add the Debuggable attribute to the module.
[<``module``: Debuggable(true, false)>]
do ()
    
type DemoClass = class end

// Get the class type to access its metadata.
let clsType = typeof<DemoClass>

// See if the Debuggable attribute is defined for this module.
let isDef = Attribute.IsDefined(clsType.Module, typeof<DebuggableAttribute>)

// Display the result.
printfn $"""The Debuggable attribute {if isDef then "is" else "is not"} defined for Module {clsType.Module.Name}."""

// If the attribute is defined, display the JIT settings.
if isDef then
    // Retrieve the attribute itself.
    let dbgAttr =
        Attribute.GetCustomAttribute(clsType.Module, typeof<DebuggableAttribute>)
        :?> DebuggableAttribute

    if dbgAttr <> null then
        printfn $"JITTrackingEnabled is {dbgAttr.IsJITTrackingEnabled}."
        printfn $"JITOptimizerDisabled is {dbgAttr.IsJITOptimizerDisabled}."            
    else
        printfn "The Debuggable attribute could not be retrieved."

// Output:
//  The Debuggable attribute is defined for Module IsDef2CS.exe.
//  JITTrackingEnabled is True.
//  JITOptimizerDisabled is False.
Imports System.Reflection
Imports System.Diagnostics

' Add the Debuggable attribute to the module.
<Module: Debuggable(True, False)> 

Module DemoModule
    Sub Main()
        ' Get the module type information to access its metadata.
        Dim modType As Type = GetType(DemoModule)
        ' See if the Debuggable attribute is defined.
        Dim isDef As Boolean = Attribute.IsDefined(modType.Module, _
                               GetType(DebuggableAttribute))
        Dim strDef As String
        If isDef = True Then
            strDef = "is"
        Else
            strDef = "is not"
        End If
        ' Display the result
        Console.WriteLine("The debuggable attribute {0} defined for " & _
                          "module {1}.", strDef, modType.Name)
        ' If the attribute is defined, display the JIT settings.
        If isDef = True Then
            ' Retrieve the attribute itself.
            Dim attr As Attribute = _
                Attribute.GetCustomAttribute(modType.Module, _
                GetType(DebuggableAttribute))
            If Not attr Is Nothing And TypeOf attr Is DebuggableAttribute Then
                Dim dbgAttr As DebuggableAttribute = _
                    CType(attr, DebuggableAttribute)
                Console.WriteLine("JITTrackingEnabled is {0}.", _
                    dbgAttr.IsJITTrackingEnabled.ToString())
                Console.WriteLine("JITOptimizerDisabled is {0}.", _
                    dbgAttr.IsJITOptimizerDisabled.ToString())
            Else
                Console.WriteLine("The Debuggable attribute could " & _
                                  "not be retrieved.")
            End If
        End If
    End Sub
End Module

' Output:
' The debuggable attribute is defined for module DemoModule.
' JITTrackingEnabled is True.
' JITOptimizerDisabled is False.

Poznámky

Vlastní element atributy se nevyhledají.

Platí pro

IsDefined(Assembly, Type)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

Určuje, zda jsou na sestavení použity nějaké vlastní atributy. Parametry určují sestavení a typ vlastního atributu, který chcete vyhledat.

public:
 static bool IsDefined(System::Reflection::Assembly ^ element, Type ^ attributeType);
public static bool IsDefined (System.Reflection.Assembly element, Type attributeType);
static member IsDefined : System.Reflection.Assembly * Type -> bool
Public Shared Function IsDefined (element As Assembly, attributeType As Type) As Boolean

Parametry

element
Assembly

Objekt odvozený z Assembly třídy, která popisuje opakovaně použitelnou kolekci modulů.

attributeType
Type

Typ nebo základní typ vlastního atributu, který chcete vyhledat.

Návraty

truepokud se pro elementpoužije vlastní atribut typu attributeType , v opačném případě false.

Výjimky

element nebo attributeType je null.

attributeType není odvozeno od Attribute.

Příklady

Následující příklad kódu znázorňuje použití parametru IsDefined, který se Assembly vezme jako parametr.

using namespace System;
using namespace System::Reflection;

// Add an AssemblyDescription attribute
[assembly:AssemblyDescription("A sample description")];
namespace IsDef1CS
{
   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = DemoClass::typeid;
         
         // Get the assembly object.
         Assembly^ assy = clsType->Assembly;
         
         // Store the assembly's name.
         String^ assyName = assy->GetName()->Name;
         
         //Type assyType = assy.GetType();
         // See if the Assembly Description is defined.
         bool isdef = Attribute::IsDefined( assy, AssemblyDescriptionAttribute::typeid );
         if ( isdef )
         {
            
            // Affirm that the attribute is defined.
            Console::WriteLine( "The AssemblyDescription attribute "
            "is defined for assembly {0}.", assyName );
            
            // Get the description attribute itself.
            AssemblyDescriptionAttribute^ adAttr = dynamic_cast<AssemblyDescriptionAttribute^>(Attribute::GetCustomAttribute( assy, AssemblyDescriptionAttribute::typeid ));
            
            // Display the description.
            if ( adAttr != nullptr )
                        Console::WriteLine( "The description is \"{0}\".", adAttr->Description );
            else
                        Console::WriteLine( "The description could not "
            "be retrieved." );
         }
         else
                  Console::WriteLine( "The AssemblyDescription attribute is not "
         "defined for assembly {0}.", assyName );
      }

   };

}


/*
 * Output:
 * The AssemblyDescription attributeis defined for assembly IsDef1CS.
 * The description is "A sample description".
 */
using System;
using System.Reflection;

// Add an AssemblyDescription attribute
[assembly: AssemblyDescription("A sample description")]
namespace IsDef1CS
{
    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(DemoClass);
            // Get the assembly object.
            Assembly assy = clsType.Assembly;
            // Store the assembly's name.
            String assyName = assy.GetName().Name;
            // See if the Assembly Description is defined.
            bool isdef = Attribute.IsDefined(assy,
                typeof(AssemblyDescriptionAttribute));
            if (isdef)
            {
                // Affirm that the attribute is defined.
                Console.WriteLine("The AssemblyDescription attribute " +
                    "is defined for assembly {0}.", assyName);
                // Get the description attribute itself.
                AssemblyDescriptionAttribute adAttr =
                    (AssemblyDescriptionAttribute)Attribute.GetCustomAttribute(
                    assy, typeof(AssemblyDescriptionAttribute));
                // Display the description.
                if (adAttr != null)
                    Console.WriteLine("The description is \"{0}\".",
                        adAttr.Description);
                else
                    Console.WriteLine("The description could not " +
                        "be retrieved.");
            }
            else
                Console.WriteLine("The AssemblyDescription attribute is not " +
                    "defined for assembly {0}.", assyName);
        }
    }
}

/*
 * Output:
 * The AssemblyDescription attribute is defined for assembly IsDef1CS.
 * The description is "A sample description".
 */
open System
open System.Reflection

// Add an AssemblyDescription attribute
[<assembly: AssemblyDescription "A sample description">]
do ()

type DemoClass = class end

// Get the class type to access its metadata.
let clsType = typeof<DemoClass>

// Get the assembly object.
let assembly = clsType.Assembly;

// Store the assembly's name.
let assemblyName = assembly.GetName().Name

// See if the Assembly Description is defined.
let isdef = 
    Attribute.IsDefined(assembly, typeof<AssemblyDescriptionAttribute>)

if isdef then
    // Affirm that the attribute is defined.
    printfn $"The AssemblyDescription attribute is defined for assembly {assemblyName}."
    
    // Get the description attribute itself.
    let adAttr =
        Attribute.GetCustomAttribute(assembly, typeof<AssemblyDescriptionAttribute>)
        :?> AssemblyDescriptionAttribute

    // Display the description.
    if adAttr <> null then
        printfn $"The description is \"{adAttr.Description}\"."
    else
        printfn $"The description could not be retrieved."
else
    printfn $"The AssemblyDescription attribute is not defined for assembly {assemblyName}."

// Output:
//  The AssemblyDescription attribute is defined for assembly IsDef1FS.
//  The description is "A sample description".
Imports System.Reflection

' Add an AssemblyDescription attribute.
<Assembly: AssemblyDescription("A sample description")> 

Module DemoModule
    Sub Main()
        ' Get the assembly for this module.
        Dim assy As System.Reflection.Assembly = GetType(DemoModule).Assembly
        ' Store the assembly name.
        Dim assyName As String = assy.GetName().Name
        ' See if the AssemblyDescription attribute is defined.
        If Attribute.IsDefined(assy, GetType(AssemblyDescriptionAttribute)) _
            Then
            ' Affirm that the attribute is defined. Assume the filename of
            ' this code example is "IsDef1VB".
            Console.WriteLine("The AssemblyDescription attribute is " & _
                "defined for assembly {0}.", assyName)
            ' Get the description attribute itself.
            Dim attr As Attribute = Attribute.GetCustomAttribute( _
                assy, GetType(AssemblyDescriptionAttribute))
            ' Display the description.
            If Not attr Is Nothing And _
                TypeOf attr Is AssemblyDescriptionAttribute Then
                Dim adAttr As AssemblyDescriptionAttribute = _
                    CType(attr, AssemblyDescriptionAttribute)
                Console.WriteLine("The description is " & _
                    Chr(34) & "{0}" & Chr(34) & ".", adAttr.Description)
            Else
                Console.WriteLine("The description could not be retrieved.")
            End If
        Else
            Console.WriteLine("The AssemblyDescription attribute is not " & _
                              "defined for assembly {0}.", assyName)
        End If
    End Sub
End Module

' Output:
' The AssemblyDescription attribute is defined for assembly IsDef1VB.
' The description is "A sample description".

Poznámky

Poznámka

Počínaje rozhraním .NET Framework verze 2.0 tato metoda vrátí true , pokud má sestavení atributy zabezpečení uložené v novém formátu metadat. Sestavení kompilovaná ve verzi 2.0 nebo novější používají nový formát. Dynamická sestavení a sestavení kompilovaná s dřívějšími verzemi rozhraní .NET Framework používají starý formát XML. Viz Generování deklarativních atributů zabezpečení.

Platí pro

IsDefined(ParameterInfo, Type)

Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs
Source:
Attribute.CoreCLR.cs

Určuje, zda jsou na parametr metody použity nějaké vlastní atributy. Parametry určují parametr metody a typ vlastního atributu, který chcete vyhledat.

public:
 static bool IsDefined(System::Reflection::ParameterInfo ^ element, Type ^ attributeType);
public static bool IsDefined (System.Reflection.ParameterInfo element, Type attributeType);
static member IsDefined : System.Reflection.ParameterInfo * Type -> bool
Public Shared Function IsDefined (element As ParameterInfo, attributeType As Type) As Boolean

Parametry

element
ParameterInfo

Objekt odvozený z ParameterInfo třídy, která popisuje parametr člena třídy.

attributeType
Type

Typ nebo základní typ vlastního atributu, který chcete vyhledat.

Návraty

truepokud se pro elementpoužije vlastní atribut typu attributeType , v opačném případě false.

Výjimky

element nebo attributeType je null.

attributeType není odvozeno od Attribute.

Příklady

Následující příklad kódu znázorňuje použití parametru IsDefined, který ParameterInfo jako parametr přebírá .

using namespace System;
using namespace System::Reflection;

namespace IsDef5CS
{
   public ref class TestClass
   {
   public:

      // Assign a ParamArray attribute to the parameter using the keyword.
      void Method1(... array<String^>^args ){}

   };

   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = TestClass::typeid;
         
         // Get the MethodInfo object for Method1.
         MethodInfo^ mInfo = clsType->GetMethod( "Method1" );
         
         // Get the ParameterInfo array for the method parameters.
         array<ParameterInfo^>^pInfo = mInfo->GetParameters();
         if ( pInfo != nullptr )
         {
            
            // See if the ParamArray attribute is defined.
            bool isDef = Attribute::IsDefined( pInfo[ 0 ], ParamArrayAttribute::typeid );
            
            // Display the result.
            Console::WriteLine( "The ParamArray attribute {0} defined for "
            "parameter {1} of method {2}.", isDef ? (String^)"is" : "is not", pInfo[ 0 ]->Name, mInfo->Name );
         }
         else
                  Console::WriteLine( "The parameters information could "
         "not be retrieved for method {0}.", mInfo->Name );
      }

   };

}

/*
 * Output:
 * The ParamArray attribute is defined for parameter args of method Method1.
 */
using System;
using System.Reflection;

namespace IsDef5CS
{
    public class TestClass
    {
        // Assign a ParamArray attribute to the parameter using the keyword.
        public void Method1(params String[] args)
        {}
    }

    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(TestClass);
            // Get the MethodInfo object for Method1.
            MethodInfo mInfo = clsType.GetMethod("Method1");
            // Get the ParameterInfo array for the method parameters.
            ParameterInfo[] pInfo = mInfo.GetParameters();
            if (pInfo != null)
            {
                // See if the ParamArray attribute is defined.
                bool isDef = Attribute.IsDefined(pInfo[0],
                                                 typeof(ParamArrayAttribute));
                // Display the result.
                Console.WriteLine("The ParamArray attribute {0} defined for " +
                                  "parameter {1} of method {2}.",
                                  isDef ? "is" : "is not",
                                  pInfo[0].Name,
                                  mInfo.Name);
            }
            else
                Console.WriteLine("The parameters information could " +
                            "not be retrieved for method {0}.", mInfo.Name);
        }
    }
}

/*
 * Output:
 * The ParamArray attribute is defined for parameter args of method Method1.
 */
open System;

type TestClass() =
    // Assign a ParamArray attribute to the parameter.
    member _.Method1([<ParamArray>] args: string[]) = ()

// Get the class type to access its metadata.
let clsType = typeof<TestClass>

// Get the MethodInfo object for Method1.
let mInfo = clsType.GetMethod "Method1"

// Get the ParameterInfo array for the method parameters.
let pInfo = mInfo.GetParameters()

if pInfo <> null then
    // See if the ParamArray attribute is defined.
    let isDef = Attribute.IsDefined(pInfo[0], typeof<ParamArrayAttribute>)

    // Display the result.
    printfn $"""The ParamArray attribute {if isDef then "is" else "is not"} defined for parameter {pInfo[0].Name} of method {mInfo.Name}."""
else
    printfn $"The parameters information could not be retrieved for method {mInfo.Name}."

// Output:
//  The ParamArray attribute is defined for parameter args of method Method1.
Imports System.Reflection

Module DemoModule
    Public Class TestClass
        ' Assign a ParamArray attribute to the parameter using the keyword.
        Public Sub Method1(ByVal ParamArray args As String())
        End Sub
    End Class

    Sub Main()
        ' Get the class type to access its metadata.
        Dim clsType As Type = GetType(TestClass)
        ' Get the MethodInfo object for Method1.
        Dim mInfo As MethodInfo = clsType.GetMethod("Method1")
        ' Get the ParameterInfo array for the method parameters.
        Dim pInfo() As ParameterInfo = mInfo.GetParameters()
        If Not pInfo(0) Is Nothing Then
            ' See if the ParamArray attribute is defined.
            Dim isDef As Boolean = Attribute.IsDefined(pInfo(0), _
                                   GetType(ParamArrayAttribute))
            Dim strDef As String
            If isDef = True Then
                strDef = "is"
            Else
                strDef = "is not"
            End If
            ' Display the result.
            Console.WriteLine("The ParamArray attribute {0} defined " & _
                              "for parameter {1} of method {2}.", _
                              strDef, pInfo(0).Name, mInfo.Name)
        Else
            Console.WriteLine("Could not retrieve parameter information " & _
                              "for method {0}.", mInfo.Name)
        End If
    End Sub
End Module

' Output:
' The ParamArray attribute is defined for parameter args of method Method1.

Poznámky

V předcích se element hledají vlastní atributy.

Platí pro