مشاركة عبر


AttributeUsage (C# و Visual Basic)

تحديد كيفية إستخدام فئة سمة مخصصة. AttributeUsage هي السمات التي يمكن تطبيقها على ملفات تعريف سمة مخصصة للتحكم بكيفية تطبيق سمة جديدة. يظهر الإعدادات الافتراضية بهذا الشكل عند التطبيق بشكل واضح:

    <System.AttributeUsage(System.AttributeTargets.All, 
                       AllowMultiple:=False, 
                       Inherited:=True)> 
    Class NewAttribute
        Inherits System.Attribute
    End Class

[System.AttributeUsage(System.AttributeTargets.All,
                   AllowMultiple = false,
                   Inherited = true)]
class NewAttribute : System.Attribute { }

في هذا المثال، يمكنك تطبيق فئة NewAttribute إلى أي سمة من التعليمات البرمجية, الوحدة ولكن يمكن تطبيق مرة واحدة فقط لكل وحدة. إنها مورثة من الفئات المشتقة عند تطبيقها إلى فئة أساسية.

تعد الوسيطاتAllowMultiple و Inherited اختيارية، بحيث تحتوي رمز هذا التأثير نفسه:

<System.AttributeUsage(System.AttributeTargets.All)> 
Class NewAttribute
    Inherits System.Attribute
End Class
[System.AttributeUsage(System.AttributeTargets.All)]
class NewAttribute : System.Attribute { }

الوسيطة الأولى AttributeUsage يجب أن تكون عنصر واحد أو أكثر من التعداد AttributeTargets . يمكن ربط العديد من أنواع الهدف مع عامل التشغيل OR كالتالي:

Imports System


...


<AttributeUsage(AttributeTargets.Property Or AttributeTargets.Field)> 
Class NewPropertyOrFieldAttribute
    Inherits Attribute
End Class
using System;


...


[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
class NewPropertyOrFieldAttribute : Attribute { }

إذا تم تعيين الوسيطةAllowMultiple إلى true ، تصبح اليمة الناتجة مكن تطبيقها إلى أكثر من مرة لكيان واحد مثل هذا:

Imports System


...


    <AttributeUsage(AttributeTargets.Class, AllowMultiple:=True)> 
    Class MultiUseAttr
        Inherits Attribute
    End Class

    <MultiUseAttr(), MultiUseAttr()> 
    Class Class1
    End Class

using System;


...


[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
class MultiUseAttr : Attribute { }

[MultiUseAttr]
[MultiUseAttr]
class Class1 { }

[MultiUseAttr, MultiUseAttr]
class Class2 { }

في هذه الحالة يمكن تطبيقMultiUseAttr بشكل متكرر بسبب تعيين AllowMultiple إلى true. كل تنسيقات عرض تطبيق السمات المتعددة صحيحة.

إذا كان Inherited تعيين إلى false ،فتكون السمة غير مورثة من الفئات المشتقة من فئة السمات. فعلى سبيل المثال:

Imports System


...


<AttributeUsage(AttributeTargets.Class, Inherited:=False)> 
Class Attr1
    Inherits Attribute
End Class

<Attr1()> 
Class BClass

End Class  

Class DClass
    Inherits BClass
End Class
using System;


...


[AttributeUsage(AttributeTargets.Class, Inherited = false)]
class Attr1 : Attribute { }

[Attr1]
class BClass { }

class DClass : BClass { }

في هذه الحالة Attr1 لا يتم تطبيق DClass خلال الوراثة.

ملاحظات

السمة AttributeUsage هي سمة واحدة الإستخدام--يتعذر تطبيقها أكثر من مرة إلى نفس الفئة. AttributeUsageهو اسم مستعار لـAttributeUsageAttribute.

لمزيد من المعلومات، راجع الوصول إلى سمات باستخدام الانعكاس (C# و Visual Basic).

مثال

يوضح المثال التالي التأثير Inherited و وسيطات AllowMultiple إلى السمة AttributeUsage و كيفية تعداد السمات المخصصة التي تم تطبيقها على فئة.

Imports System


...


    ' Create some custom attributes:
    <AttributeUsage(System.AttributeTargets.Class, Inherited:=False)> 
    Class A1
        Inherits System.Attribute
    End Class

    <AttributeUsage(System.AttributeTargets.Class)> 
    Class A2
        Inherits System.Attribute
    End Class    

    <AttributeUsage(System.AttributeTargets.Class, AllowMultiple:=True)> 
    Class A3
        Inherits System.Attribute
    End Class


    ' Apply custom attributes to classes:
    <A1(), A2()> 
    Class BaseClass

    End Class

    <A3(), A3()> 
    Class DerivedClass
        Inherits BaseClass
    End Class


    Public Class TestAttributeUsage
        Sub Main()
            Dim b As New BaseClass
            Dim d As New DerivedClass
            ' Display custom attributes for each class.
            Console.WriteLine("Attributes on Base Class:")
            Dim attrs() As Object = b.GetType().GetCustomAttributes(True)

            For Each attr In attrs
                Console.WriteLine(attr)
            Next

            Console.WriteLine("Attributes on Derived Class:")
            attrs = d.GetType().GetCustomAttributes(True)
            For Each attr In attrs
                Console.WriteLine(attr)
            Next            
        End Sub
    End Class

using System;


...


// Create some custom attributes:
[AttributeUsage(System.AttributeTargets.Class, Inherited = false)]
class A1 : System.Attribute { }

[AttributeUsage(System.AttributeTargets.Class)]
class A2 : System.Attribute { }

[AttributeUsage(System.AttributeTargets.Class, AllowMultiple = true)]
class A3 : System.Attribute { }

// Apply custom attributes to classes:
[A1, A2]
class BaseClass { }

[A3, A3]
class DerivedClass : BaseClass { }

public class TestAttributeUsage
{
    static void Main()
    {
        BaseClass b = new BaseClass();
        DerivedClass d = new DerivedClass();

        // Display custom attributes for each class.
        Console.WriteLine("Attributes on Base Class:");
        object[] attrs = b.GetType().GetCustomAttributes(true);
        foreach (Attribute attr in attrs)
        {
            Console.WriteLine(attr);
        }

        Console.WriteLine("Attributes on Derived Class:");
        attrs = d.GetType().GetCustomAttributes(true);
        foreach (Attribute attr in attrs)
        {
            Console.WriteLine(attr);
        }
    }
}

عينة مخرجات:

Attributes on Base Class:
A1
A2
Attributes on Derived Class:
A3
A3
A2

راجع أيضًا:

المرجع

انعكاس (C# و Visual Basic)

دعم Visual Basic و Visual C#‎

لإنشاء سمات مخصصة (C# و Visual Basic)

الوصول إلى سمات باستخدام الانعكاس (C# و Visual Basic)

Attribute

System.Reflection

المبادئ

دليل البرمجة لـ #C

لتوسيع بيانات التعريف باستخدام السمات

موارد أخرى

دليل البرمجة Visual Basic