The Labs \ Source Viewer \ SSCLI \ System.Diagnostics \ SwitchAttribute

  1. //------------------------------------------------------------------------------
  2. // <copyright file="SwitchAttribute.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. using System;
  16. using System.Reflection;
  17. using System.Collections;
  18. namespace System.Diagnostics
  19. {
  20.    
  21.     [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Property)]
  22.     public sealed class SwitchAttribute : Attribute
  23.     {
  24.         private Type type;
  25.         private string name;
  26.         private string description;
  27.        
  28.         public SwitchAttribute(string switchName, Type switchType)
  29.         {
  30.             SwitchName = switchName;
  31.             SwitchType = switchType;
  32.         }
  33.        
  34.         public string SwitchName {
  35.             get { return name; }
  36.             set {
  37.                 if (value == null)
  38.                     throw new ArgumentNullException("value");
  39.                 if (value.Length == 0)
  40.                     throw new ArgumentException(SR.GetString(SR.InvalidNullEmptyArgument, "value"), "value");
  41.                
  42.                 name = value;
  43.             }
  44.         }
  45.        
  46.         public Type SwitchType {
  47.             get { return type; }
  48.             set {
  49.                 if (value == null)
  50.                     throw new ArgumentNullException("value");
  51.                 type = value;
  52.             }
  53.         }
  54.        
  55.         public string SwitchDescription {
  56.             get { return description; }
  57.             set { description = value; }
  58.         }
  59.        
  60.         public static SwitchAttribute[] GetAll(Assembly assembly)
  61.         {
  62.             if (assembly == null)
  63.                 throw new ArgumentNullException("assembly");
  64.            
  65.             ArrayList switchAttribs = new ArrayList();
  66.            
  67.             object[] attribs = assembly.GetCustomAttributes(typeof(SwitchAttribute), false);
  68.             switchAttribs.AddRange(attribs);
  69.            
  70.             Type[] types = assembly.GetTypes();
  71.             for (int i = 0; i < types.Length; i++)
  72.                 GetAllRecursive(types[i], switchAttribs);
  73.            
  74.             SwitchAttribute[] ret = new SwitchAttribute[switchAttribs.Count];
  75.             switchAttribs.CopyTo(ret, 0);
  76.             return ret;
  77.         }
  78.        
  79.         static void GetAllRecursive(Type type, ArrayList switchAttribs)
  80.         {
  81.             GetAllRecursive((MemberInfo)type, switchAttribs);
  82.             MemberInfo[] members = type.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static);
  83.             for (int i = 0; i < members.Length; i++) {
  84.                 // ignore Types here. They will get covered by the top level assembly.GetTypes
  85.                 if (!(members[i] is Type))
  86.                     GetAllRecursive(members[i], switchAttribs);
  87.             }
  88.            
  89.         }
  90.        
  91.         static void GetAllRecursive(MemberInfo member, ArrayList switchAttribs)
  92.         {
  93.             object[] attribs = member.GetCustomAttributes(typeof(SwitchAttribute), false);
  94.             switchAttribs.AddRange(attribs);
  95.         }
  96.        
  97.        
  98.     }
  99. }

Developer Fusion