C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet617/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   613   614   615   616   617   618   619   620   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

RemarkAttribute

. The 


Remark

 property 



cannot

 be 


used as a named parameter because it is read-only. Remember that only public, read-write 

fields and properties can be used as named parameters.

A public, read-write property can be used as a named parameter in the same way as 

a field. For example, here an auto-implemented 



int

 property called 



Priority

 is added to 



RemarkAttribute

:

// Use a property as a named attribute parameter.



using System;

using System.Reflection;

[AttributeUsage(AttributeTargets.All)]

public class RemarkAttribute : Attribute {

  string pri_remark; // underlies Remark property

  public string Supplement; // this is a named parameter

  public RemarkAttribute(string comment) {

    pri_remark = comment;

    Supplement = "None";

    Priority = 1;

  }

  public string Remark {



    get {

      return pri_remark;

    }

  }


www.freepdf-books.com


490

 

P a r t   I :  



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

  // Use a property as a named parameter.

  public int Priority { get; set; }

}

[RemarkAttribute("This class uses an attribute.",



                 Supplement = "This is additional info.",

                 Priority = 10)]

class UseAttrib {

  // ...


}

class NamedParamDemo {

  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);

    }

    // Retrieve the RemarkAttribute.



    Type tRemAtt = typeof(RemarkAttribute);

    RemarkAttribute ra = (RemarkAttribute)

          Attribute.GetCustomAttribute(t, tRemAtt);

    Console.Write("Remark: ");

    Console.WriteLine(ra.Remark);

    Console.Write("Supplement: ");

    Console.WriteLine(ra.Supplement);

    Console.WriteLine("Priority: " + ra.Priority);

  }

}

The output is shown here:



Attributes in UseAttrib: RemarkAttribute

Remark: This class uses an attribute.

Supplement: This is additional info.

Priority: 10

There is one point of interest in the program. Notice the attribute specified before 

UseAttrib

 that is shown here:

[RemarkAttribute("This class uses an attribute.",

                 Supplement = "This is additional info.",

                 Priority = 10)]

The named attributes 



Supplement

 and 


Priority

 are 


not

 in any special order. These two 

assignments can be reversed without any change to the attribute.

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   613   614   615   616   617   618   619   620   ...   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