MemberInfo.IsDefined(Type, Boolean) Yöntem

Tanım

Türetilmiş bir sınıfta geçersiz kılındığında, belirtilen türde veya türetilmiş türlerinden bir veya daha fazla özniteliğin bu üyeye uygulanıp uygulanmadığını gösterir.

C#
public abstract bool IsDefined(Type attributeType, bool inherit);

Parametreler

attributeType
Type

Aranacak özel özniteliğin türü. Arama, türetilmiş türleri içerir.

inherit
Boolean

true özniteliklerini bulmak üzere bu üyenin devralma zincirinde arama yapmak için; aksi takdirde , false. Özellikler ve olaylar için bu parametre yoksayılır.

Döndürülenler

true bu üyeye attributeType bir veya daha fazla örneği veya türetilmiş türlerinden biri uygulanırsa; aksi takdirde, false.

Uygulamalar

Örnekler

Aşağıdaki örnek, belirtilen özniteliğin belirtilen üyeye uygulanıp uygulanmadığını belirler.

C#
using System;
using System.Reflection;

// Define a custom attribute with one named parameter.
[AttributeUsage(AttributeTargets.All)]
public class MyAttribute : Attribute
{
    private string myName;
    public MyAttribute(string name)
    {
        myName = name;
    }
    public string Name
    {
        get
        {
            return myName;
        }
    }
}

// Define a class that has the custom attribute associated with one of its members.
public class MyClass1
{
    [MyAttribute("This is an example attribute.")]
    public void MyMethod(int i)
    {
        return;
    }
}

public class MemberInfo_GetCustomAttributes_IsDefined
{
    public static void Main()
    {
        try
        {
            // Get the type of MyClass1.
            Type myType = typeof(MyClass1);
            // Get the members associated with MyClass1.
            MemberInfo[] myMembers = myType.GetMembers();

            // Display the attributes for each of the members of MyClass1.
            for(int i = 0; i < myMembers.Length; i++)
            {
                // Display the attribute if it is of type MyAttribute.
                if(myMembers[i].IsDefined(typeof(MyAttribute), false))
                {
                    Object[] myAttributes = myMembers[i].GetCustomAttributes(typeof(MyAttribute), false);
                    Console.WriteLine("\nThe attributes of type MyAttribute for the member {0} are: \n",
                        myMembers[i]);
                    for(int j = 0; j < myAttributes.Length; j++)
                        // Display the value associated with the attribute.
                        Console.WriteLine("The value of the attribute is : \"{0}\"",
                            ((MyAttribute)myAttributes[j]).Name);
                }
            }
        }
        catch(Exception e)
        {
            Console.WriteLine("An exception occurred: {0}", e.Message);
        }
    }
}

Açıklamalar

Bu yöntem, özellikler ve olaylar için parametresini yoksayar inherit . Devralma zincirinde özellikler ve olaylardaki öznitelikleri aramak için yönteminin uygun aşırı yüklemelerini Attribute.IsDefined kullanın.

Not

.NET Framework sürüm 2.0'da, bir türün, yöntemin veya oluşturucunun yeni meta veri biçiminde depolanan güvenlik öznitelikleri varsa bu yöntem döndürürtrue. Sürüm 2.0 ile derlenen derlemeler bu biçimi kullanır. .NET Framework önceki sürümleriyle derlenen dinamik derlemeler ve derlemeler eski XML biçimini kullanır. Bkz. Bildirim Temelli Güvenlik Özniteliklerini Yayma.

Şunlara uygulanır

Ürün Sürümler
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1