ScenarioCategory.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace UICatalog;
  6. /// <summary>Defines the category names used to categorize a <see cref="Scenario"/></summary>
  7. [AttributeUsage (AttributeTargets.Class, AllowMultiple = true)]
  8. public class ScenarioCategory (string name) : System.Attribute
  9. {
  10. /// <summary>Static helper function to get the <see cref="Scenario"/> Categories given a Type</summary>
  11. /// <param name="t"></param>
  12. /// <returns>list of category names</returns>
  13. public static List<string> GetCategories (Type t)
  14. {
  15. return GetCustomAttributes (t)
  16. .ToList ()
  17. .Where (a => a is ScenarioCategory)
  18. .Select<System.Attribute, string> (a => ((ScenarioCategory)a).Name)
  19. .ToList ();
  20. }
  21. /// <summary>Static helper function to get the <see cref="Scenario"/> Name given a Type</summary>
  22. /// <param name="t"></param>
  23. /// <returns>Name of the category</returns>
  24. public static string GetName (Type t)
  25. {
  26. if (GetCustomAttributes (t).FirstOrDefault (a => a is ScenarioMetadata) is ScenarioMetadata { } metadata)
  27. {
  28. return metadata.Name;
  29. }
  30. return string.Empty;
  31. }
  32. /// <summary>Category Name</summary>
  33. public string Name { get; set; } = name;
  34. }