C# 0 The Complete Reference


UseAttrib  class has the  RemarkAttribute



Download 4,07 Mb.
Pdf ko'rish
bet611/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   607   608   609   610   611   612   613   614   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

UseAttrib

 class has the 



RemarkAttribute

, to obtain a reference to the 



RemarkAttribute

, you can use a sequence like this:

// Get a MemberInfo instance associated with a

// class that has the RemarkAttribute.

Type t = typeof(UseAttrib);

// Retrieve the RemarkAttribute.

Type tRemAtt = typeof(RemarkAttribute);

RemarkAttribute ra = (RemarkAttribute)

      Attribute.GetCustomAttribute(t, tRemAtt);

This sequence works because 



MemberInfo

 is a base class of 



Type

. Thus, 


t

 is a 


MemberInfo

instance.

Once you have a reference to an attribute, you can access its members. This makes 

information associated with an attribute available to a program that uses an element to 

which an attribute is attached. For example, the following statement displays the 

Remark

property:

Console.WriteLine(ra.Remark);

www.freepdf-books.com




486

 

P a r t   I :  



T h e   C #   L a n g u a g e

The following program puts together all of the pieces and demonstrates the use of 



RemarkAttribute

:

// A simple attribute example.



using System;

using System.Reflection;

[AttributeUsage(AttributeTargets.All)]

public class RemarkAttribute : Attribute {

  string pri_remark; // underlies Remark property

  public RemarkAttribute(string comment) {

    pri_remark = comment;

  }


  public string Remark {

    get {

      return pri_remark;

    }


  }

}

[RemarkAttribute("This class uses an attribute.")]



class UseAttrib {

  // ...


}

class AttribDemo {

  static void Main() {

    Type t = typeof(UseAttrib);

    Console.Write("Attributes in " + t.Name + ": ");

    object[] attribs = t.GetCustomAttributes(false);

    foreach(object o in attribs) {

      Console.WriteLine(o);

    }

    Console.Write("Remark: ");



    // Retrieve the RemarkAttribute.

    Type tRemAtt = typeof(RemarkAttribute);

    RemarkAttribute ra = (RemarkAttribute)

          Attribute.GetCustomAttribute(t, tRemAtt);

    Console.WriteLine(ra.Remark);

  }


}

The output from the program is shown here:

Attributes in UseAttrib: RemarkAttribute

Remark: This class uses an attribute.

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   607   608   609   610   611   612   613   614   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish