Attribute.IsDefaultAttribute Methode

Definitie

Wanneer deze wordt overschreven in een afgeleide klasse, geeft u aan of de waarde van dit exemplaar de standaardwaarde is voor de afgeleide klasse.

public:
 virtual bool IsDefaultAttribute();
public virtual bool IsDefaultAttribute();
abstract member IsDefaultAttribute : unit -> bool
override this.IsDefaultAttribute : unit -> bool
Public Overridable Function IsDefaultAttribute () As Boolean

Retouren

true als dit exemplaar het standaardkenmerk voor de klasse is; anders, false.

Voorbeelden

Het volgende codevoorbeeld illustreert het gebruik van IsDefaultAttribute.

using System;
using System.Reflection;

namespace DefAttrCS
{
    // An enumeration of animals. Start at 1 (0 = uninitialized).
    public enum Animal
    {
        // Pets.
        Dog = 1,
        Cat,
        Bird,
    }

    // A custom attribute to allow a target to have a pet.
    public class AnimalTypeAttribute : Attribute
    {
        // The constructor is called when the attribute is set.
        public AnimalTypeAttribute(Animal pet)
        {
            thePet = pet;
        }

        // Provide a default constructor and make Dog the default.
        public AnimalTypeAttribute()
        {
            thePet = Animal.Dog;
        }

        // Keep a variable internally ...
        protected Animal thePet;

        // .. and show a copy to the outside world.
        public Animal Pet
        {
            get { return thePet; }
            set { thePet = Pet; }
        }

        // Override IsDefaultAttribute to return the correct response.
        public override bool IsDefaultAttribute()
        {
            if (thePet == Animal.Dog)
                return true;

            return false;
        }
    }

    public class TestClass
    {
        // Use the default constructor.
        [AnimalType]
        public void Method1()
        {}
    }

    class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(TestClass);
            // Get type information for the method.
            MethodInfo mInfo = clsType.GetMethod("Method1");
            // Get the AnimalType attribute for the method.
            AnimalTypeAttribute atAttr =
                (AnimalTypeAttribute)Attribute.GetCustomAttribute(mInfo,
                typeof(AnimalTypeAttribute));
            // Check to see if the default attribute is applied.
            Console.WriteLine("The attribute {0} for method {1} in class {2}",
                atAttr.Pet, mInfo.Name, clsType.Name);
            Console.WriteLine("{0} the default for the AnimalType attribute.",
                atAttr.IsDefaultAttribute() ? "is" : "is not");
        }
    }
}
open System

// An enumeration of animals. Start at 1 (0 = uninitialized).
type Animal =
    | Dog = 1
    | Cat = 2
    | Bird = 3

// A custom attribute to allow a target to have a pet.
type AnimalTypeAttribute(pet) =
    inherit Attribute()

    member val Pet = pet

    // Override IsDefaultAttribute to return the correct response.
    override _.IsDefaultAttribute() =
        pet = Animal.Dog

    // Provide a default constructor and make Dog the default.
    new() = AnimalTypeAttribute Animal.Dog

type TestClass() =
    // Use the default constructor.
    [<AnimalType>]
    member _.Method1() = ()

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

// Get type information for the method.
let mInfo = clsType.GetMethod "Method1"

// Get the AnimalType attribute for the method.
let atAttr = 
    Attribute.GetCustomAttribute(mInfo, typeof<AnimalTypeAttribute>) 
    :?> AnimalTypeAttribute

// Check to see if the default attribute is applied.
printf $"The attribute {atAttr.Pet} for method {mInfo.Name} in class {clsType.Name} "
printfn $"""{if atAttr.IsDefaultAttribute() then "is" else "is not"} the default for the AnimalType attribute."""

// Output:
//     The attribute Dog for method Method1 in class TestClass is the default for the AnimalType attribute.
Imports System.Reflection

Module DemoModule

    ' An enumeration of animals. Start at 1 (0 = uninitialized).
    Enum Animal
        ' Pets
        Dog = 1
        Cat
        Bird
    End Enum

    ' Visual Basic requires that the AttributeUsage be specified.
    ' A custom attribute to allow a target to have a pet.
    <AttributeUsage(AttributeTargets.Method)> _
    Public Class AnimalTypeAttribute
        Inherits Attribute

        ' The constructor is called when the attribute is set.
        Public Sub New(ByVal animal As Animal)
            Me.thePet = animal
        End Sub

        ' Provide a default constructor and make Dog the default.
        Public Sub New()
            thePet = Animal.Dog
        End Sub

        ' Keep a variable internally ...
        Protected thePet As Animal

        ' .. and show a copy to the outside world.
        Public Property Pet() As Animal
            Get
                Return thePet
            End Get
            Set(ByVal Value As Animal)
                thePet = Value
            End Set
        End Property

        ' Override IsDefaultAttribute to return the correct response.
        Public Overrides Function IsDefaultAttribute() As Boolean
            If thePet = Animal.Dog Then
                Return True
            Else
                Return False
            End If
        End Function

    End Class

    Public Class TestClass
        ' Use the default constructor.
        <AnimalType()> _
        Public Sub Method1()
        End Sub

    End Class

    Sub Main()
        ' Get the class type to access its metadata.
        Dim clsType As Type = GetType(TestClass)
        ' Get type information for the method.
        Dim mInfo As MethodInfo = clsType.GetMethod("Method1")
        ' Get the AnimalType attribute for the method.
        Dim attr As Attribute = Attribute.GetCustomAttribute(mInfo, _
            GetType(AnimalTypeAttribute))
        If Not attr Is Nothing And TypeOf attr Is AnimalTypeAttribute Then
            ' Convert the attribute to the required type.
            Dim atAttr As AnimalTypeAttribute = _
                CType(attr, AnimalTypeAttribute)
            Dim strDef As String
            ' Check to see if the default attribute is applied.
            If atAttr.IsDefaultAttribute() Then
                strDef = "is"
            Else
                strDef = "is not"
            End If
            ' Display the result.
            Console.WriteLine("The attribute {0} for method {1} " & _
                    "in class {2}", atAttr.Pet.ToString(), mInfo.Name, _
                    clsType.Name)
            Console.WriteLine("{0} the default for the AnimalType " & _
                    "attribute.", strDef)
        End If
    End Sub
End Module

Opmerkingen

De standaard implementatie van deze klasse retourneert falseen moet worden geïmplementeerd in de afgeleide klasse om nuttig te zijn voor die klasse.

De implementatie van deze methode in een afgeleide klasse vergelijkt de waarde van dit exemplaar met een standaardstandaardwaarde en retourneert vervolgens een Booleaanse waarde die aangeeft of de waarde van dit exemplaar gelijk is aan de standaardwaarde. De standaardwaarde wordt doorgaans gecodeerd als een constante in de implementatie of wordt programmatisch opgeslagen in een veld dat door de implementatie wordt gebruikt.

Van toepassing op