导航

  1 using System;
2 using System.Reflection;
3
4 class Module1
5 {
6
7 public static void Main()
8 {
9 // This variable holds the amount of indenting that
10 // should be used when displaying each line of information.
11 Int32 indent = 0;
12 // Display information about the EXE assembly.
13 Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
14 Display(indent, "Assembly identity={0}", a.FullName);
15 Display(indent+1, "Codebase={0}", a.CodeBase);
16
17 // Display the set of assemblies our assemblies reference.
18
19 Display(indent, "Referenced assemblies:");
20 foreach (AssemblyName an in a.GetReferencedAssemblies() )
21 {
22 Display(indent + 1, "Name={0}, Version={1}, Culture={2}, PublicKey token={3}", an.Name, an.Version, an.CultureInfo.Name, (BitConverter.ToString (an.GetPublicKeyToken())));
23 }
24 Display(indent, "");
25
26 // Display information about each assembly loading into this AppDomain.
27 foreach (Assembly b in AppDomain.CurrentDomain.GetAssemblies())
28 {
29 Display(indent, "Assembly: {0}", b);
30
31 // Display information about each module of this assembly.
32 foreach ( Module m in b.GetModules(true) )
33 {
34 Display(indent+1, "Module: {0}", m.Name);
35 }
36
37 // Display information about each type exported from this assembly.
38
39 indent += 1;
40 foreach ( Type t in b.GetExportedTypes() )
41 {
42 Display(0, "");
43 Display(indent, "Type: {0}", t);
44
45 // For each type, show its members & their custom attributes.
46
47 indent += 1;
48 foreach (MemberInfo mi in t.GetMembers() )
49 {
50 Display(indent, "Member: {0}", mi.Name);
51 DisplayAttributes(indent, mi);
52
53 // If the member is a method, display information about its parameters.
54
55 if (mi.MemberType==MemberTypes.Method)
56 {
57 foreach ( ParameterInfo pi in ((MethodInfo) mi).GetParameters() )
58 {
59 Display(indent+1, "Parameter: Type={0}, Name={1}", pi.ParameterType, pi.Name);
60 }
61 }
62
63 // If the member is a property, display information about the property's accessor methods.
64 if (mi.MemberType==MemberTypes.Property)
65 {
66 foreach ( MethodInfo am in ((PropertyInfo) mi).GetAccessors() )
67 {
68 Display(indent+1, "Accessor method: {0}", am);
69 }
70 }
71 }
72 indent -= 1;
73 }
74 indent -= 1;
75 }
76 }
77
78 // Displays the custom attributes applied to the specified member.
79 public static void DisplayAttributes(Int32 indent, MemberInfo mi)
80 {
81 // Get the set of custom attributes; if none exist, just return.
82 object[] attrs = mi.GetCustomAttributes(false);
83 if (attrs.Length==0) {return;}
84
85 // Display the custom attributes applied to this member.
86 Display(indent+1, "Attributes:");
87 foreach ( object o in attrs )
88 {
89 Display(indent+2, "{0}", o.ToString());
90 }
91 }
92
93 // Display a formatted string indented by the specified amount.
94 public static void Display(Int32 indent, string format, params object[] param)
95
96 {
97 Console.Write(new string(' ', indent*2));
98 Console.WriteLine(format, param);
99 }
100 }
101
102 //The output shown below is abbreviated.
103 //
104 //Assembly identity=ReflectionCS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
105 // Codebase=file:///C:/Documents and Settings/test/My Documents/Visual Studio 2005/Projects/Reflection/Reflection/obj/Debug/Reflection.exe
106 //Referenced assemblies:
107 // Name=mscorlib, Version=2.0.0.0, Culture=, PublicKey token=B7-7A-5C-56-19-34-E0-89
108 //
109 //Assembly: mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
110 // Module: mscorlib.dll
111 // Module: mscorlib.dll
112 // Module: mscorlib.dll
113 // Module: mscorlib.dll
114 // Module: mscorlib.dll
115 // Module: mscorlib.dll
116 // Module: mscorlib.dll
117 // Module: mscorlib.dll
118 // Module: mscorlib.dll
119 // Module: mscorlib.dll
120 // Module: mscorlib.dll
121 // Module: mscorlib.dll
122 // Module: mscorlib.dll
123 // Module: mscorlib.dll
124 //
125 // Type: System.Object
126 // Member: GetType
127 // Member: ToString
128 // Member: Equals
129 // Parameter: Type=System.Object, Name=obj
130 // Member: Equals
131 // Parameter: Type=System.Object, Name=objA
132 // Parameter: Type=System.Object, Name=objB
133 // Member: ReferenceEquals
134 // Attributes:
135 // System.Runtime.ConstrainedExecution.ReliabilityContractAttribute
136 // Parameter: Type=System.Object, Name=objA
137 // Parameter: Type=System.Object, Name=objB
138 // Member: GetHashCode
139 // Member: .ctor
140 // Attributes:
141 // System.Runtime.ConstrainedExecution.ReliabilityContractAttribute
142 //
143 // Type: System.ICloneable
144 // Member: Clone
145 //
146 // Type: System.Collections.IEnumerable
147 // Member: GetEnumerator
148 // Attributes:
149 // System.Runtime.InteropServices.DispIdAttribute
150 //
151 // Type: System.Collections.ICollection
152 // Member: CopyTo
153 // Parameter: Type=System.Array, Name=array
154 // Parameter: Type=System.Int32, Name=index
155 // Member: get_Count
156 // Member: get_SyncRoot
157 // Member: get_IsSynchronized
158 // Member: Count
159 // Accessor method: Int32 get_Count()
160 // Member: SyncRoot
161 // Accessor method: System.Object get_SyncRoot()
162 // Member: IsSynchronized
163 // Accessor method: Boolean get_IsSynchronized()
164 //
165 // Type: System.Collections.IList
166 // Member: get_Item
167 // Parameter: Type=System.Int32, Name=index
168 // Member: set_Item
169 // Parameter: Type=System.Int32, Name=index
170 // Parameter: Type=System.Object, Name=value
171 // Member: Add
172 // Parameter: Type=System.Object, Name=value
173 // Member: Contains
174 // Parameter: Type=System.Object, Name=value
175 // Member: Clear
176 // Member: get_IsReadOnly
177 // Member: get_IsFixedSize
178 // Member: IndexOf
179 // Parameter: Type=System.Object, Name=value
180 // Member: Insert
181 // Parameter: Type=System.Int32, Name=index
182 // Parameter: Type=System.Object, Name=value
183 // Member: Remove
184 // Parameter: Type=System.Object, Name=value
185 // Member: RemoveAt
186 // Parameter: Type=System.Int32, Name=index
187 // Member: Item
188 // Accessor method: System.Object get_Item(Int32)
189 // Accessor method: Void set_Item(Int32, System.Object)
190 // Member: IsReadOnly
191 // Accessor method: Boolean get_IsReadOnly()
192 // Member: IsFixedSize
193 // Accessor method: Boolean get_IsFixedSize()
194 //
195 // Type: System.Array
196 // Member: IndexOf
197 // Parameter: Type=T[], Name=array
198 // Parameter: Type=T, Name=value
199 // Member: AsReadOnly
200 // Parameter: Type=T[], Name=array
201 // Member: Resize
202 // Attributes:
203 // System.Runtime.ConstrainedExecution.ReliabilityContractAttribute
204 // Parameter: Type=T[]&, Name=array
205 // Parameter: Type=System.Int32, Name=newSize
206 // Member: BinarySearch
207 // Attributes:
208 // System.Runtime.ConstrainedExecution.ReliabilityContractAttribute
209 // Parameter: Type=T[], Name=array
210 // Parameter: Type=T, Name=value
211 // Member: BinarySearch
212 // Attributes:
213 // System.Runtime.ConstrainedExecution.ReliabilityContractAttribute
214 // Parameter: Type=T[], Name=array
215 // Parameter: Type=T, Name=value
216 // Parameter: Type=System.Collections.Generic.IComparer`1[T], Name=comparer


原文:【MSDN文章】memberinfo类