mscorlib(4.0.0.0) API with additions
SwitchAttribute.cs
1 using System.Collections;
2 using System.Reflection;
3 
4 namespace System.Diagnostics
5 {
7  [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event)]
8  public sealed class SwitchAttribute : Attribute
9  {
10  private Type type;
11 
12  private string name;
13 
14  private string description;
15 
22  public string SwitchName
23  {
24  get
25  {
26  return name;
27  }
28  set
29  {
30  if (value == null)
31  {
32  throw new ArgumentNullException("value");
33  }
34  if (value.Length == 0)
35  {
36  throw new ArgumentException(SR.GetString("InvalidNullEmptyArgument", "value"), "value");
37  }
38  name = value;
39  }
40  }
41 
46  public Type SwitchType
47  {
48  get
49  {
50  return type;
51  }
52  set
53  {
54  if (value == null)
55  {
56  throw new ArgumentNullException("value");
57  }
58  type = value;
59  }
60  }
61 
64  public string SwitchDescription
65  {
66  get
67  {
68  return description;
69  }
70  set
71  {
72  description = value;
73  }
74  }
75 
79  public SwitchAttribute(string switchName, Type switchType)
80  {
81  SwitchName = switchName;
82  SwitchType = switchType;
83  }
84 
90  public static SwitchAttribute[] GetAll(Assembly assembly)
91  {
92  if (assembly == null)
93  {
94  throw new ArgumentNullException("assembly");
95  }
96  ArrayList arrayList = new ArrayList();
97  object[] customAttributes = assembly.GetCustomAttributes(typeof(SwitchAttribute), inherit: false);
98  arrayList.AddRange(customAttributes);
99  Type[] types = assembly.GetTypes();
100  for (int i = 0; i < types.Length; i++)
101  {
102  GetAllRecursive(types[i], arrayList);
103  }
104  SwitchAttribute[] array = new SwitchAttribute[arrayList.Count];
105  arrayList.CopyTo(array, 0);
106  return array;
107  }
108 
109  private static void GetAllRecursive(Type type, ArrayList switchAttribs)
110  {
111  GetAllRecursive((MemberInfo)type, switchAttribs);
112  MemberInfo[] members = type.GetMembers(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
113  for (int i = 0; i < members.Length; i++)
114  {
115  if (!(members[i] is Type))
116  {
117  GetAllRecursive(members[i], switchAttribs);
118  }
119  }
120  }
121 
122  private static void GetAllRecursive(MemberInfo member, ArrayList switchAttribs)
123  {
124  object[] customAttributes = member.GetCustomAttributes(typeof(SwitchAttribute), inherit: false);
125  switchAttribs.AddRange(customAttributes);
126  }
127  }
128 }
Obtains information about the attributes of a member and provides access to member metadata.
Definition: MemberInfo.cs:14
string SwitchDescription
Gets or sets the description of the switch.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Represents the base class for custom attributes.
Definition: Attribute.cs:15
virtual int Count
Gets the number of elements actually contained in the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2255
BindingFlags
Specifies flags that control binding and the way in which the search for members and types is conduct...
Definition: BindingFlags.cs:10
Definition: __Canon.cs:3
abstract object [] GetCustomAttributes(bool inherit)
When overridden in a derived class, returns an array of all custom attributes applied to this member.
MemberInfo [] GetMembers()
Returns all the public members of the current T:System.Type.
Definition: Type.cs:2089
virtual object [] GetCustomAttributes(bool inherit)
Gets all the custom attributes for this assembly.
Definition: Assembly.cs:1041
string SwitchName
Gets or sets the display name of the switch.
virtual void AddRange(ICollection c)
Adds the elements of an T:System.Collections.ICollection to the end of the T:System....
Definition: ArrayList.cs:2397
Represents an assembly, which is a reusable, versionable, and self-describing building block of a com...
Definition: Assembly.cs:22
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
SwitchAttribute(string switchName, Type switchType)
Initializes a new instance of the T:System.Diagnostics.SwitchAttribute class, specifying the name and...
virtual void CopyTo(Array array)
Copies the entire T:System.Collections.ArrayList to a compatible one-dimensional T:System....
Definition: ArrayList.cs:2516
Type SwitchType
Gets or sets the type of the switch.
virtual Type [] GetTypes()
Gets the types defined in this assembly.
Definition: Assembly.cs:941
AttributeTargets
Specifies the application elements on which it is valid to apply an attribute.
The exception that is thrown when one of the arguments provided to a method is not valid.
Identifies a switch used in an assembly, class, or member.
static SwitchAttribute [] GetAll(Assembly assembly)
Returns all switch attributes for the specified assembly.
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14