Yurttas/PL/OOL/CS/F/07/00/A01.cs
Jump to navigation
Jump to search
1/**
2 * Copyright(C) 2004
3 * All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc..
4 *
5 * Permission to use, copy, modify, and distribute this
6 * software and its documentation for EDUCATIONAL purposes
7 * and without fee is hereby granted provided that this
8 * copyright notice appears in all copies.
9 *
10 * @date : August 1, 2004.
11 * @author : Salih Yurttas, Yi Yang.
12 */
13
14
15using System;
16using System.Reflection;
17
18namespace Attribute00
19{
20
21 // create custom attribute to be assigned to class members
22 [AttributeUsage(AttributeTargets.Class |
23 AttributeTargets.Constructor |
24 AttributeTargets.Field |
25 AttributeTargets.Method |
26 AttributeTargets.Property,
27 AllowMultiple = true)]
28 public class BugFixAttribute : System.Attribute
29 {
30 // private member data
31 private int bugID;
32 private string programmer;
33 private string date;
34
35 // attribute constructor for
36 // positional parameters
37 public BugFixAttribute(int bugID,
38 string programmer,
39 string date)
40 {
41 this.bugID = bugID;
42 this.programmer = programmer;
43 this.date = date;
44 }
45
46 // accessor
47 public int BugID
48 {
49 get
50 {
51 return bugID;
52 }
53 }
54
55 // accessor
56 public string Programmer
57 {
58 get
59 {
60 return programmer;
61 }
62 }
63
64 // accessor
65 public string Date
66 {
67 get
68 {
69 return date;
70 }
71 }
72 }
73
74 // ********* assign the attributes to the class ********
75 [BugFixAttribute(100,"SY","01/03/03")]
76 [BugFixAttribute(107,"Haiwen","01/04/04")]
77 [BugFixAttribute(103,"Yi","07/31/04")]
78 public class A01
79 {
80 [BugFixAttribute(103,"Yi","08/01/04")]
81 public double DoFunc1(double p1)
82 {
83 return p1 + DoFunc2(p1);
84 }
85
86 public double DoFunc2(double p1)
87 {
88 return p1/3;
89 }
90 }
91
92 public class Test00
93 {
94 public static void PrintAttributes(BugFixAttribute bfa)
95 {
96 Console.WriteLine("BugID: {0}", bfa.BugID);
97 Console.WriteLine("Programmer: {0}", bfa.Programmer);
98 Console.WriteLine("Date: {0}", bfa.Date);
99 }
100
101 public static void Main(string[] args)
102 {
103 // get the member information and use it to
104 // retrieve the custom attributes
105 MemberInfo inf = typeof(A01);
106
107 object[] attributes;
108
109 attributes = inf.GetCustomAttributes(typeof(BugFixAttribute),
110 false);
111
112 //print out the attributes for class
113 Console.WriteLine("printing class attributes");
114 foreach(Object attribute in attributes)
115 PrintAttributes((BugFixAttribute)attribute);
116
117 //get the customer attributes for the methods
118 Type theType = Type.GetType("Attibute00.A01");
119 MemberInfo[] methodInfo = theType.GetMembers();
120
121 //print out the attributes for member if have
122 Console.WriteLine("\nprinting member attributes");
123 foreach(MemberInfo info in methodInfo)
124 {
125 attributes = info.GetCustomAttributes(false);
126 foreach(object attribute in attributes)
127 PrintAttributes((BugFixAttribute)attribute);
128 }
129 }
130 }
131
132}