ReflectionUtils.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System.Reflection;
  2. using Quadarax.Foundation.Core.Reflection;
  3. namespace qdr.fnd.core.test.Utils
  4. {
  5. public static class ReflectionUtils
  6. {
  7. #region *** Properties operations ***
  8. public static bool HasProperty(Type type, string propertyName, bool includeInternals = false)
  9. {
  10. return GetProperty(type, propertyName, includeInternals)!=null;
  11. }
  12. public static bool HasPropertySetter(Type type, string propertyName, bool includeInternals = false)
  13. {
  14. var pinfo = GetProperty(type, propertyName, includeInternals);
  15. if (pinfo != null)
  16. {
  17. return pinfo.CanWrite;
  18. }
  19. throw new InvalidOperationException($"Cannot find property '{propertyName}' in type '{type.Name}'.");
  20. }
  21. public static bool HasPropertyGetter(Type type, string propertyName, bool includeInternals = false)
  22. {
  23. var pinfo = GetProperty(type, propertyName, includeInternals);
  24. if (pinfo != null)
  25. {
  26. return pinfo.CanRead;
  27. }
  28. throw new InvalidOperationException($"Cannot find property '{propertyName}' in type '{type.Name}'.");
  29. }
  30. public static PropertyInfo? GetProperty(Type type, string propertyName, bool includeInternals = false)
  31. {
  32. if (type == null) throw new ArgumentNullException(nameof(type));
  33. if (string.IsNullOrEmpty(propertyName)) throw new ArgumentNullException(nameof(propertyName));
  34. var bindings = includeInternals ? BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic : BindingFlags.Instance | BindingFlags.Public;
  35. return type.GetProperties(bindings).FirstOrDefault(x=>string.Equals(x.Name, propertyName, StringComparison.InvariantCulture));
  36. }
  37. public static TAttribute? GetPropertyAttribute<TAttribute>(Type type, string propertyName, bool includeInherited = false) where TAttribute : Attribute
  38. {
  39. var pinfo = GetProperty(type, propertyName, true);
  40. if (pinfo != null)
  41. {
  42. return AttributeQuery<TAttribute>.Find(pinfo, includeInherited);
  43. }
  44. throw new InvalidOperationException($"Cannot find property '{propertyName}' in type '{type.Name}'.");
  45. }
  46. public static bool HasPropertyAttribute<TAttribute>(Type type, string propertyName, bool includeInherited = false) where TAttribute : Attribute
  47. {
  48. return GetPropertyAttribute<TAttribute>(type, propertyName, includeInherited)!=null;
  49. }
  50. #endregion
  51. #region *** Class operations ***
  52. public static bool HasInterface(Type type, Type interfaceType)
  53. {
  54. if (type == null) throw new ArgumentNullException(nameof(type));
  55. if (interfaceType == null) throw new ArgumentNullException(nameof(interfaceType));
  56. return type.IsAssignableFrom(interfaceType);
  57. }
  58. public static bool HasInterfaceAll(Type type, params Type[] interfaceTypes)
  59. {
  60. var result = true;
  61. foreach (var interfaceType in interfaceTypes)
  62. {
  63. result = result && HasInterface(type, interfaceType);
  64. }
  65. return result;
  66. }
  67. public static bool HasInterfaceAny(Type type, params Type[] interfaceTypes)
  68. {
  69. var result = false;
  70. foreach (var interfaceType in interfaceTypes)
  71. {
  72. result = result || HasInterface(type, interfaceType);
  73. }
  74. return result;
  75. }
  76. #endregion
  77. }
  78. }