|
| 1 | +using System.ComponentModel; |
| 2 | +using System.Reflection; |
| 3 | + |
| 4 | +namespace BlazorExpress.ChartJS.Demo.RCL; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Extension methods for <see cref="PropertyInfo" />. |
| 8 | +/// </summary> |
| 9 | +public static class PropertyInfoExtenstions |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Get event callback return type. |
| 13 | + /// </summary> |
| 14 | + /// <param name="propertyInfo"></param> |
| 15 | + /// <returns>Returns list of component event callbacks.</returns> |
| 16 | + public static string GetEventCallbackReturnType(this PropertyInfo propertyInfo) |
| 17 | + { |
| 18 | + HashSet<string> arguments = new(); |
| 19 | + if (propertyInfo.PropertyType.IsGenericType) |
| 20 | + { |
| 21 | + Type[] genericArguments = propertyInfo.PropertyType.GetGenericArguments(); |
| 22 | + if (genericArguments.Length > 0) |
| 23 | + { |
| 24 | + foreach (Type genericArgument in genericArguments) |
| 25 | + arguments.Add(genericArgument.GetCSharpTypeName()); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + return arguments.Count > 0 ? $"EventCallback<{string.Join(",", arguments)}>" : "EventCallback"; |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Get parameter type name. |
| 34 | + /// </summary> |
| 35 | + /// <param name="propertyInfo"></param> |
| 36 | + /// <returns>string</returns> |
| 37 | + public static string GetParameterTypeName(this PropertyInfo propertyInfo) |
| 38 | + { |
| 39 | + var parameterTypeNameAttribute = propertyInfo.GetCustomAttributes(typeof(ParameterTypeNameAttribute), false).FirstOrDefault() as ParameterTypeNameAttribute; |
| 40 | + return parameterTypeNameAttribute?.TypeName ?? null!; |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Get added version of a property. |
| 45 | + /// </summary> |
| 46 | + /// <param name="propertyInfo"></param> |
| 47 | + /// <returns>string</returns> |
| 48 | + public static string GetPropertyAddedVersion(this PropertyInfo propertyInfo) |
| 49 | + { |
| 50 | + var addedVersionAttribute = propertyInfo.GetCustomAttributes(typeof(AddedVersionAttribute), false).FirstOrDefault() as AddedVersionAttribute; |
| 51 | + return addedVersionAttribute?.Version!; |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Get default value of a property. |
| 56 | + /// </summary> |
| 57 | + /// <param name="propertyInfo"></param> |
| 58 | + /// <returns>string</returns> |
| 59 | + public static string GetPropertyDefaultValue(this PropertyInfo propertyInfo) |
| 60 | + { |
| 61 | + var defaultValueAttribute = propertyInfo.GetCustomAttributes(typeof(DefaultValueAttribute), false).FirstOrDefault() as DefaultValueAttribute; |
| 62 | + return defaultValueAttribute?.Value?.ToString() ?? "null"; |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Get property description. |
| 67 | + /// </summary> |
| 68 | + /// <param name="propertyInfo"></param> |
| 69 | + /// <returns>string</returns> |
| 70 | + public static string GetPropertyDescription(this PropertyInfo propertyInfo) |
| 71 | + { |
| 72 | + var descriptionAttribute = propertyInfo.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() as DescriptionAttribute; |
| 73 | + return descriptionAttribute?.Description ?? string.Empty; |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Get property type name. |
| 78 | + /// </summary> |
| 79 | + /// <param name="propertyInfo"></param> |
| 80 | + /// <returns>string</returns> |
| 81 | + public static string GetPropertyTypeName(this PropertyInfo propertyInfo) |
| 82 | + { |
| 83 | + var propertyTypeName = propertyInfo.Name; |
| 84 | + if (string.IsNullOrWhiteSpace(propertyTypeName)) |
| 85 | + return string.Empty; |
| 86 | + |
| 87 | + if (propertyTypeName.Contains(StringConstants.PropertyTypeNameInt16, StringComparison.InvariantCulture)) |
| 88 | + propertyTypeName = StringConstants.PropertyTypeNameInt16CSharpTypeKeyword; |
| 89 | + |
| 90 | + else if (propertyTypeName.Contains(StringConstants.PropertyTypeNameInt32, StringComparison.InvariantCulture)) |
| 91 | + propertyTypeName = StringConstants.PropertyTypeNameInt32CSharpTypeKeyword; |
| 92 | + |
| 93 | + else if (propertyTypeName.Contains(StringConstants.PropertyTypeNameInt64, StringComparison.InvariantCulture)) |
| 94 | + propertyTypeName = StringConstants.PropertyTypeNameInt64CSharpTypeKeyword; |
| 95 | + |
| 96 | + else if (propertyTypeName.Contains(StringConstants.PropertyTypeNameChar, StringComparison.InvariantCulture)) |
| 97 | + propertyTypeName = StringConstants.PropertyTypeNameCharCSharpTypeKeyword; |
| 98 | + |
| 99 | + else if (propertyTypeName.Contains(StringConstants.PropertyTypeNameStringComparison, StringComparison.InvariantCulture)) |
| 100 | + propertyTypeName = StringConstants.PropertyTypeNameStringComparisonCSharpTypeKeyword; |
| 101 | + |
| 102 | + else if (propertyTypeName.Contains(StringConstants.PropertyTypeNameString, StringComparison.InvariantCulture)) |
| 103 | + propertyTypeName = StringConstants.PropertyTypeNameStringCSharpTypeKeyword; |
| 104 | + |
| 105 | + else if (propertyTypeName.Contains(StringConstants.PropertyTypeNameSingle, StringComparison.InvariantCulture)) // float |
| 106 | + propertyTypeName = StringConstants.PropertyTypeNameSingleCSharpTypeKeyword; |
| 107 | + |
| 108 | + else if (propertyTypeName.Contains(StringConstants.PropertyTypeNameDecimal, StringComparison.InvariantCulture)) |
| 109 | + propertyTypeName = StringConstants.PropertyTypeNameDecimalCSharpTypeKeyword; |
| 110 | + |
| 111 | + else if (propertyTypeName.Contains(StringConstants.PropertyTypeNameDouble, StringComparison.InvariantCulture)) |
| 112 | + propertyTypeName = StringConstants.PropertyTypeNameDoubleCSharpTypeKeyword; |
| 113 | + |
| 114 | + else if (propertyTypeName.Contains(StringConstants.PropertyTypeNameDateOnly, StringComparison.InvariantCulture)) |
| 115 | + propertyTypeName = StringConstants.PropertyTypeNameDateOnlyCSharpTypeKeyword; |
| 116 | + |
| 117 | + else if (propertyTypeName.Contains(StringConstants.PropertyTypeNameDateTime, StringComparison.InvariantCulture)) |
| 118 | + propertyTypeName = StringConstants.PropertyTypeNameDateTimeCSharpTypeKeyword; |
| 119 | + |
| 120 | + else if (propertyTypeName.Contains(StringConstants.PropertyTypeNameBoolean, StringComparison.InvariantCulture)) |
| 121 | + propertyTypeName = StringConstants.PropertyTypeNameBooleanCSharpTypeKeyword; |
| 122 | + |
| 123 | + //else if (propertyType!.IsEnum) |
| 124 | + // propertyTypeName = StringConstants.PropertyTypeNameEnumCSharpTypeKeyword; |
| 125 | + |
| 126 | + else if (propertyTypeName.Contains(StringConstants.PropertyTypeNameGuid, StringComparison.InvariantCulture)) |
| 127 | + propertyTypeName = StringConstants.PropertyTypeNameGuidCSharpTypeKeyword; |
| 128 | + |
| 129 | + return propertyTypeName; |
| 130 | + } |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// Determines whether the specified property is an EventCallback or EventCallback<T>. |
| 134 | + /// </summary> |
| 135 | + /// <param name="propertyInfo"></param> |
| 136 | + /// <returns>bool</returns> |
| 137 | + /// <exception cref="ArgumentNullException"></exception> |
| 138 | + public static bool IsEventCallbackProperty(this PropertyInfo propertyInfo) |
| 139 | + { |
| 140 | + if (propertyInfo == null) |
| 141 | + { |
| 142 | + throw new ArgumentNullException(nameof(propertyInfo)); |
| 143 | + } |
| 144 | + |
| 145 | + // Check for EventCallback |
| 146 | + if (propertyInfo.PropertyType == typeof(EventCallback)) |
| 147 | + { |
| 148 | + return true; |
| 149 | + } |
| 150 | + |
| 151 | + // Check for EventCallback<T> |
| 152 | + if (propertyInfo.PropertyType.IsGenericType && |
| 153 | + propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(EventCallback<>)) |
| 154 | + { |
| 155 | + return true; |
| 156 | + } |
| 157 | + |
| 158 | + return false; |
| 159 | + } |
| 160 | + |
| 161 | + /// <summary> |
| 162 | + /// Returns true if the property is required. Otherwise, false. |
| 163 | + /// </summary> |
| 164 | + /// <param name="propertyInfo"></param> |
| 165 | + /// <returns>bool</returns> |
| 166 | + public static bool IsPropertyRequired(this PropertyInfo propertyInfo) |
| 167 | + { |
| 168 | + var editorRequiredAttribute = propertyInfo.GetCustomAttributes(typeof(EditorRequiredAttribute), false).FirstOrDefault() as EditorRequiredAttribute; |
| 169 | + return editorRequiredAttribute is not null; |
| 170 | + } |
| 171 | +} |
0 commit comments