Share via


Attribute.IsDefaultAttribute メソッド

派生クラスによってオーバーライドされた場合、このインスタンスの値が派生クラスの既定値かどうかを示す情報を返します。

Public Overridable Function IsDefaultAttribute() As Boolean
[C#]
public virtual bool IsDefaultAttribute();
[C++]
public: virtual bool IsDefaultAttribute();
[JScript]
public function IsDefaultAttribute() : Boolean;

戻り値

このインスタンスがクラスの既定の属性の場合は true 。それ以外の場合は false

解説

このクラスの既定の実装は、 false を返します。この既定の実装は、そのクラスで使用できるように派生クラスに実装されている必要があります。

派生クラスでのこのメソッドの実装は、このインスタンスの値と、なんらかの方法で取得された標準の既定値とを比較します。次に、このインスタンスの値が標準の既定値と等しいかどうかを示すブール値を返します。通常、標準値はこの実装の定数として指定されているか、またはこの実装が使用するフィールドにプログラムによって格納されています。

使用例

IsDefaultAttribute の使用方法については、次のコード例を参照してください。

 
Imports System
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

[C#] 
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");
        }
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;

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

// A custom attribute to allow a target to have a pet.
public __gc class AnimalTypeAttribute : public 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.
   AnimalTypeAttribute() 
   {
      thePet = Animal::Dog;
   }

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

   // .. and show a copy to the outside world.
public:
   __property Animal get_Pet() { return thePet; }
   __property void set_Pet( Animal value ) { thePet = value; }

   // Override IsDefaultAttribute to return the correct response.
   bool IsDefaultAttribute() 
   {
      return thePet == Animal::Dog;
   }
};

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

int main() 
{
   // Get the class type to access its metadata.
   Type* clsType = __typeof(TestClass);
   // Get type information for the method.
   MethodInfo* mInfo = clsType->GetMethod(S"Method1");
   // Get the AnimalType attribute for the method.
   AnimalTypeAttribute* atAttr = dynamic_cast<AnimalTypeAttribute*>
      (Attribute::GetCustomAttribute(mInfo,__typeof(AnimalTypeAttribute)));
   // Check to see if the default attribute is applied.
   Console::WriteLine(S"The attribute {0} for method {1} in class {2}",
      __box(atAttr->Pet), mInfo->Name, clsType->Name); 
   Console::WriteLine(S"{0} the default for the AnimalType attribute.", 
      atAttr->IsDefaultAttribute() ? S"is" : S"is not");
}

[JScript] 
import System;
import System.Reflection;

package DefAttrJS {
    // 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.
    AttributeUsage(AttributeTargets.Method) public class AnimalTypeAttribute extends Attribute {
        // The constructor is called when the attribute is set.
        public function AnimalTypeAttribute(pet : Animal) {
            thePet = pet;
        }

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

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

        // .. and show a copy to the outside world.
        public function get Pet() : Animal {
            return thePet;         
        }

        public function set Pet(value : Animal) {        
            thePet = value;
        }

        // Override IsDefaultAttribute to return the correct response.
        public override function IsDefaultAttribute() : boolean {
            return thePet == Animal.Dog;
        }
    }

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

    class DemoClass {
        static function Main() : void  {
            // Get the class type to access its metadata.
            var clsType : Type  = TestClass;
            // Get type information for the method.
            var mInfo : MethodInfo = clsType.GetMethod("Method1");
            // Get the AnimalType attribute for the method.
            var atAttr : AnimalTypeAttribute = 
                AnimalTypeAttribute(Attribute.GetCustomAttribute(mInfo, 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");
        }
    }
}

DefAttrJS.DemoClass.Main();

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

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

Attribute クラス | Attribute メンバ | System 名前空間