|
class Class1 { static void Main(string[] args) { // Создание объекта, ассоциированного с типом AClass1 Type t= typeof(AClass1); MyAttribute MyAttr = (MyAttribute) Attribute.GetCustomAttribute( t, typeof (MyAttribute)); if(null == MyAttr) {Console.WriteLine("Атрибут не найден");} else { Console.WriteLine("Атрибут name = {0}, атрибут kod = {1}" , MyAttr.Name, // Возвращает значение // поля Name атрибута MyAttribute MyAttr.Kod); } } } [AttributeUsage(AttributeTargets.All)] public class MyAttribute : System.Attribute { private string name; private int kod; public MyAttribute(string name) {this.name = name; this.kod = 10; } public string Name { get { return name;} } public int Kod { get { return kod; } set {kod=value; } } } [My("NameOfClass", Kod=123)] public class AClass1 { public AClass1() { } private int [] iArr = new int[10]; public int this[int ind1] { get { return iArr[ind1];} set { iArr[ind1]= value; } } } |
| Листинг 17.1. |
| Закрыть окно |