The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Extensions.cs 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. namespace Teknik.Utilities
  8. {
  9. public static class Extensions
  10. {
  11. public static string GetDescription<T>(this T value)
  12. {
  13. FieldInfo fi = value.GetType().GetField(value.ToString());
  14. DescriptionAttribute[] attributes = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
  15. if (attributes != null && attributes.Any())
  16. {
  17. return attributes.First().Description;
  18. }
  19. return value.ToString();
  20. }
  21. public static string GetDisplayName<T>(this T value)
  22. {
  23. FieldInfo fi = value.GetType().GetField(value.ToString());
  24. DisplayNameAttribute[] attributes = fi.GetCustomAttributes(typeof(DisplayNameAttribute), false) as DisplayNameAttribute[];
  25. if (attributes != null && attributes.Any())
  26. {
  27. return attributes.First().DisplayName;
  28. }
  29. return value.ToString();
  30. }
  31. public static bool IsReadOnly<T>(this T value)
  32. {
  33. FieldInfo fi = value.GetType().GetField(value.ToString());
  34. ReadOnlyAttribute[] attributes = fi.GetCustomAttributes(typeof(ReadOnlyAttribute), false) as ReadOnlyAttribute[];
  35. if (attributes != null && attributes.Any())
  36. {
  37. return attributes.First().IsReadOnly;
  38. }
  39. return false;
  40. }
  41. }
  42. }