code_junkie

How can I get a collection of all the colors in System.Drawing.Color?

Nov 12th, 2011
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. string[] colors = Enum.GetNames(typeof(System.Drawing.KnownColor));
  2.  
  3. Type colorType = typeof(System.Drawing.Color);
  4.  
  5. PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);
  6.  
  7. foreach (System.Reflection.PropertyInfo c in propInfoList) {
  8. Console.WriteLine(c.Name);
  9. }
  10.  
  11. foreach (KnownColor knownColor in Enum.GetValues(typeof(KnownColor)))
  12. {
  13. Trace.WriteLine(string.Format("{0}", knownColor));
  14. }
  15.  
  16. System.Collections.Generic.List<string> colors =
  17. new System.Collections.Generic.List<string>();
  18. Type t = typeof(System.Drawing.Color);
  19. System.Reflection.PropertyInfo[] infos = t.GetProperties();
  20. foreach (System.Reflection.PropertyInfo info in infos)
  21. if (info.PropertyType == typeof(System.Drawing.Color))
  22. colors.Add(info.Name);
  23.  
  24. using System.Collections.Generic;
  25. using System.Drawing;
  26. using System.Linq;
  27. ...
  28. static IEnumerable<Color> GetSystemColors() {
  29. Type type = typeof(Color);
  30. return type.GetProperties().Where(info => info.PropertyType == type).Select(info => (Color)info.GetValue(null, null));
  31. }
Add Comment
Please, Sign In to add comment