| | | 1 | | using PropertyGridHelpers.Attributes; |
| | | 2 | | using PropertyGridHelpers.Enums; |
| | | 3 | | using System; |
| | | 4 | | using System.Collections; |
| | | 5 | | using System.ComponentModel; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.Linq; |
| | | 9 | | using System.Reflection; |
| | | 10 | | using System.Resources; |
| | | 11 | | using System.Threading; |
| | | 12 | | using System.Windows.Forms; |
| | | 13 | | |
| | | 14 | | namespace PropertyGridHelpers.Support |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Functions used to Support the processes provided by the PropertyGridHelpers library. |
| | | 18 | | /// </summary> |
| | | 19 | | public static class Support |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Gets the resources names from the assembly where the enum is located. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="enumType">Type of the enum.</param> |
| | | 25 | | /// <returns> |
| | | 26 | | /// Returns a string array containing the names of all resources in the assembly where the passed in enum type |
| | | 27 | | /// is located. |
| | | 28 | | /// </returns> |
| | | 29 | | /// <exception cref="ArgumentNullException">Thrown when the parameter <paramref name="enumType"/> is null</excep |
| | | 30 | | /// <exception cref="ArgumentException">Throw when the parameter <paramref name="enumType"/> is not an Enum type |
| | | 31 | | /// <!-- IntelliSense Only --> |
| | | 32 | | /// See <see href="https://github.com/dparvin/PropertyGridHelpers/wiki/73ec243d-2005-9d6b-8d20-bfc12895eec6">Pro |
| | | 33 | | public static string[] GetResourcesNames(Type enumType) |
| | 20 | 34 | | { |
| | | 35 | | #if NET5_0_OR_GREATER |
| | 12 | 36 | | ArgumentNullException.ThrowIfNull(enumType); |
| | | 37 | | #else |
| | 16 | 38 | | if (enumType == null) |
| | 16 | 39 | | throw new ArgumentNullException(nameof(enumType)); |
| | | 40 | | #endif |
| | | 41 | | |
| | 24 | 42 | | if (!enumType.IsEnum) |
| | 20 | 43 | | throw new ArgumentException("The provided type must be an enum.", nameof(enumType)); |
| | | 44 | | |
| | | 45 | | // Get the assembly where the enum type is declared |
| | 20 | 46 | | var assembly = enumType.Assembly; |
| | | 47 | | |
| | | 48 | | // Get all embedded resources in the assembly |
| | 20 | 49 | | var resourceNames = assembly.GetManifestResourceNames(); |
| | | 50 | | |
| | 20 | 51 | | return resourceNames; |
| | 12 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Analyzes the resources embedded within a given assembly and prints details about their type and structure. |
| | | 56 | | /// </summary> |
| | | 57 | | /// <param name="assembly">The assembly to inspect for embedded resources.</param> |
| | | 58 | | /// <exception cref="ArgumentNullException"> |
| | | 59 | | /// Thrown if <paramref name="assembly"/> is <c>null</c>. |
| | | 60 | | /// </exception> |
| | | 61 | | /// <remarks> |
| | | 62 | | /// This method examines the specified <paramref name="assembly"/> and identifies: |
| | | 63 | | /// <list type="bullet"> |
| | | 64 | | /// <item><description>Embedded resources.</description></item> |
| | | 65 | | /// <item><description>Compiled resource files (<c>.resources</c> files).</description></item> |
| | | 66 | | /// </list> |
| | | 67 | | /// The results are written to the standard output (console). |
| | | 68 | | /// <para><b>Example Output:</b></para> |
| | | 69 | | /// <pre> |
| | | 70 | | /// Checking resources in assembly: |
| | | 71 | | /// Embedded Resources: - MyNamespace.MyResource.txt (Embedded Resource) |
| | | 72 | | /// |
| | | 73 | | /// Resource Files: |
| | | 74 | | /// - MyNamespace.Strings.resources (Resource File) |
| | | 75 | | /// -> WelcomeMessage: System.String |
| | | 76 | | /// -> AppVersion: System.Int32 |
| | | 77 | | /// </pre> |
| | | 78 | | /// |
| | | 79 | | /// <para>If a compiled resource file (<c>.resources</c>) is found, this method also |
| | | 80 | | /// attempts to deserialize its contents and print the key-value pairs along with their data types.</para> |
| | | 81 | | /// |
| | | 82 | | /// <para><b>Note:</b> This method is intended primarily for debugging and inspection purposes. It may not be |
| | | 83 | | /// suitable for use in production applications.</para> |
| | | 84 | | /// </remarks> |
| | | 85 | | public static void CheckResourceType(Assembly assembly) |
| | 16 | 86 | | { |
| | | 87 | | #if NET5_0_OR_GREATER |
| | 8 | 88 | | ArgumentNullException.ThrowIfNull(assembly); |
| | | 89 | | #else |
| | 16 | 90 | | if (assembly == null) |
| | 16 | 91 | | throw new ArgumentNullException(nameof(assembly)); |
| | | 92 | | #endif |
| | | 93 | | |
| | 20 | 94 | | Console.WriteLine("Checking resources in assembly:"); |
| | | 95 | | |
| | | 96 | | // Check for embedded resources |
| | 20 | 97 | | var embeddedResources = assembly.GetManifestResourceNames(); |
| | 20 | 98 | | Console.WriteLine("\nEmbedded Resources:"); |
| | 60 | 99 | | foreach (var resource in embeddedResources) |
| | 24 | 100 | | { |
| | 32 | 101 | | Console.WriteLine($"- {resource} (Embedded Resource)"); |
| | 24 | 102 | | } |
| | | 103 | | |
| | | 104 | | // Check for resource files (.resx compiled to .resources) |
| | 20 | 105 | | Console.WriteLine("\nResource Files:"); |
| | 52 | 106 | | foreach (var resourceName in embeddedResources.Where( |
| | 36 | 107 | | r => r.EndsWith(".resources", StringComparison.InvariantCulture))) |
| | 20 | 108 | | { |
| | 28 | 109 | | Console.WriteLine($"- {resourceName} (Resource File)"); |
| | | 110 | | #if NET5_0_OR_GREATER |
| | 12 | 111 | | using var stream = assembly.GetManifestResourceStream(resourceName); |
| | 12 | 112 | | if (stream != null) |
| | 12 | 113 | | { |
| | 12 | 114 | | using var reader = new System.Resources.Extensions.DeserializingResourceReader(stream); |
| | 180 | 115 | | foreach (DictionaryEntry entry in reader) |
| | 72 | 116 | | Console.WriteLine($" -> {entry.Key}: {entry.Value.GetType().FullName}"); |
| | 12 | 117 | | } |
| | | 118 | | #else |
| | | 119 | | |
| | 16 | 120 | | using (var stream = assembly.GetManifestResourceStream(resourceName)) |
| | 16 | 121 | | if (stream != null) |
| | 16 | 122 | | using (var reader = new ResourceReader(stream)) |
| | 16 | 123 | | foreach (DictionaryEntry entry in reader) |
| | 16 | 124 | | Console.WriteLine($" -> {entry.Key}: {entry.Value.GetType().FullName}"); |
| | | 125 | | #endif |
| | 20 | 126 | | } |
| | 20 | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Retrieves a localized string from a resource file based on the specified resource key. |
| | | 131 | | /// </summary> |
| | | 132 | | /// <param name="resourceKey">The key identifying the resource string.</param> |
| | | 133 | | /// <param name="culture">The culture.</param> |
| | | 134 | | /// <param name="resourceSource">The type of the resource class that contains the resource file.</param> |
| | | 135 | | /// <returns> |
| | | 136 | | /// The localized string corresponding to <paramref name="resourceKey" /> from the specified <paramref name="res |
| | | 137 | | /// key itself. |
| | | 138 | | /// </returns> |
| | | 139 | | /// <exception cref="ArgumentNullException">Thrown if <paramref name="resourceKey" /> or <paramref name="resourc |
| | | 140 | | /// <remarks> |
| | | 141 | | /// This method uses a <see cref="ResourceManager" /> to retrieve the localized string based on the current |
| | | 142 | | /// culture. If the resource key does not exist in the specified resource file, the method returns the key |
| | | 143 | | /// itself instead of throwing an exception. |
| | | 144 | | /// </remarks> |
| | | 145 | | /// <example> |
| | | 146 | | /// Example usage: |
| | | 147 | | /// <code> |
| | | 148 | | /// string message = GetResourceString("WelcomeMessage", typeof(Resources.Messages)); |
| | | 149 | | /// Console.WriteLine(message); // Outputs localized message or "WelcomeMessage" if not found |
| | | 150 | | /// </code> |
| | | 151 | | /// </example> |
| | | 152 | | public static string GetResourceString(string resourceKey, CultureInfo culture, Type resourceSource) |
| | 64 | 153 | | { |
| | 72 | 154 | | if (string.IsNullOrEmpty(resourceKey)) |
| | 20 | 155 | | throw new ArgumentNullException(nameof(resourceKey)); |
| | | 156 | | #if NET8_0_OR_GREATER |
| | 52 | 157 | | ArgumentNullException.ThrowIfNull(resourceSource); |
| | | 158 | | #else |
| | | 159 | | |
| | 16 | 160 | | if (resourceSource == null) |
| | 16 | 161 | | throw new ArgumentNullException(nameof(resourceSource)); |
| | | 162 | | #endif |
| | | 163 | | |
| | 64 | 164 | | var resourceManager = new ResourceManager(resourceSource); |
| | | 165 | | |
| | | 166 | | try |
| | 56 | 167 | | { |
| | 64 | 168 | | var result = resourceManager.GetString(resourceKey, culture ?? CultureInfo.CurrentCulture); |
| | 60 | 169 | | if (result == null) |
| | 12 | 170 | | Debug.WriteLine($"[Localization] Missing resource key: '{resourceKey}' in {resourceSource.FullName}" |
| | | 171 | | |
| | 60 | 172 | | return result ?? resourceKey; |
| | | 173 | | } |
| | 20 | 174 | | catch (MissingManifestResourceException) |
| | 12 | 175 | | { |
| | 20 | 176 | | return resourceKey; // Fallback if resource file is missing |
| | | 177 | | } |
| | 64 | 178 | | } |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Determines the resource path based on the specified property or related data type, |
| | | 182 | | /// filtered by the specified <see cref="ResourceUsage" />. |
| | | 183 | | /// </summary> |
| | | 184 | | /// <param name="context"> |
| | | 185 | | /// The type descriptor context, which provides metadata about the property and its container. |
| | | 186 | | /// </param> |
| | | 187 | | /// <param name="type"> |
| | | 188 | | /// The data type associated with the resource, typically an enum or a property type. |
| | | 189 | | /// </param> |
| | | 190 | | /// <param name="resourceUsage"> |
| | | 191 | | /// The kind of resource to resolve (e.g., <see cref="ResourceUsage.Strings" />, |
| | | 192 | | /// <see cref="ResourceUsage.Images" />). This helps determine the most appropriate resource path |
| | | 193 | | /// when multiple paths are defined. |
| | | 194 | | /// </param> |
| | | 195 | | /// <param name="throwOnError"> |
| | | 196 | | /// if set to <c>true</c> throw on error when the Dynamic Path Attribute is not setup correctly. |
| | | 197 | | /// </param> |
| | | 198 | | /// <returns> |
| | | 199 | | /// A string containing the resource path based on the provided property or type. |
| | | 200 | | /// If no applicable attributes are found, the method returns the default resource path: |
| | | 201 | | /// <c>"Properties.Resources"</c>. |
| | | 202 | | /// </returns> |
| | | 203 | | /// <exception cref="ArgumentNullException"> |
| | | 204 | | /// Thrown if <paramref name="type" /> is <c>null</c>. |
| | | 205 | | /// </exception> |
| | | 206 | | /// <exception cref="ArgumentException"> |
| | | 207 | | /// Thrown if <paramref name="resourceUsage" /> is <see cref="ResourceUsage.None" />. |
| | | 208 | | /// </exception> |
| | | 209 | | /// <remarks> |
| | | 210 | | /// This method searches for the resource path using the following order of precedence: |
| | | 211 | | /// <list type="number"> |
| | | 212 | | /// <item> |
| | | 213 | | /// If the property has a <see cref="DynamicPathSourceAttribute" /> matching the given |
| | | 214 | | /// <paramref name="resourceUsage" />, it retrieves the path from the referenced property. |
| | | 215 | | /// </item> |
| | | 216 | | /// <item> |
| | | 217 | | /// If the property or its type has a <see cref="ResourcePathAttribute" /> matching the specified |
| | | 218 | | /// <paramref name="resourceUsage" />, it uses the defined path. |
| | | 219 | | /// </item> |
| | | 220 | | /// <item> |
| | | 221 | | /// If the type (or its underlying nullable type) is an enumeration and has a matching |
| | | 222 | | /// <see cref="ResourcePathAttribute" />, it uses the associated path. |
| | | 223 | | /// </item> |
| | | 224 | | /// <item> |
| | | 225 | | /// If none of the above conditions are met, it defaults to <c>"Properties.Resources"</c>. |
| | | 226 | | /// </item> |
| | | 227 | | /// </list> |
| | | 228 | | /// </remarks> |
| | | 229 | | /// <example> |
| | | 230 | | /// Example usage with static path: |
| | | 231 | | /// <code> |
| | | 232 | | /// [ResourcePath("Custom.Resources", resourceUsage: ResourceUsage.Strings)] |
| | | 233 | | /// public enum MyEnum |
| | | 234 | | /// { |
| | | 235 | | /// Value1, |
| | | 236 | | /// Value2 |
| | | 237 | | /// } |
| | | 238 | | /// |
| | | 239 | | /// string path = GetResourcePath(null, typeof(MyEnum), ResourceUsage.Strings); |
| | | 240 | | /// Console.WriteLine(path); // Outputs: "Custom.Resources" |
| | | 241 | | /// </code> |
| | | 242 | | /// |
| | | 243 | | /// Example usage with dynamic path: |
| | | 244 | | /// <code> |
| | | 245 | | /// [DynamicPathSource(nameof(MyResourcePath), ResourceUsage.Images)] |
| | | 246 | | /// public MyEnum ImageSelector { get; set; } |
| | | 247 | | /// |
| | | 248 | | /// public string MyResourcePath => "Dynamic.Image.Resources"; |
| | | 249 | | /// </code> |
| | | 250 | | /// </example> |
| | | 251 | | public static string GetResourcePath( |
| | | 252 | | ITypeDescriptorContext context, |
| | | 253 | | Type type, |
| | | 254 | | ResourceUsage resourceUsage = ResourceUsage.All, |
| | | 255 | | bool throwOnError = false) |
| | 124 | 256 | | { |
| | | 257 | | // Check the context for a dynamic path |
| | 132 | 258 | | if (context?.Instance != null && |
| | 132 | 259 | | context.PropertyDescriptor != null) |
| | 56 | 260 | | { |
| | | 261 | | // 1. Check for DynamicPathSourceAttribute for the given usage |
| | 64 | 262 | | var dynamicAttr = DynamicPathSourceAttribute.Get(context, resourceUsage); |
| | 64 | 263 | | if (dynamicAttr != null) |
| | 28 | 264 | | { |
| | 36 | 265 | | var sourceProp = context.Instance.GetType().GetProperty(dynamicAttr.PathPropertyName); |
| | 36 | 266 | | if (sourceProp == null) |
| | 16 | 267 | | { |
| | 24 | 268 | | if (throwOnError) |
| | 20 | 269 | | throw new MissingMemberException($"The '{dynamicAttr.PathPropertyName}' property is not defi |
| | 12 | 270 | | } |
| | | 271 | | else |
| | 20 | 272 | | { |
| | 28 | 273 | | if (sourceProp.PropertyType == typeof(string)) |
| | 20 | 274 | | return sourceProp.GetValue(context.Instance, null) as string; |
| | 24 | 275 | | else if (throwOnError) |
| | 20 | 276 | | throw new InvalidOperationException($"The property '{dynamicAttr.PathPropertyName}' on type |
| | 12 | 277 | | } |
| | | 278 | | |
| | 16 | 279 | | } |
| | | 280 | | // 2. Check for ResourcePathAttribute for the given usage |
| | 52 | 281 | | var pathAttr = ResourcePathAttribute.Get(context, resourceUsage); |
| | 52 | 282 | | if (pathAttr != null) |
| | 32 | 283 | | return pathAttr.ResourcePath; |
| | 28 | 284 | | } |
| | | 285 | | |
| | | 286 | | // 3. Check for ResourcePathAttribute on the enum type (if applicable) |
| | 104 | 287 | | if (type != null) |
| | 96 | 288 | | { |
| | 104 | 289 | | var enumType = Nullable.GetUnderlyingType(type) ?? type; |
| | 104 | 290 | | if (enumType.IsEnum) |
| | 80 | 291 | | { |
| | 88 | 292 | | var enumAttrs = enumType.GetCustomAttributes(typeof(ResourcePathAttribute), true) |
| | 88 | 293 | | .OfType<ResourcePathAttribute>(); |
| | 144 | 294 | | var enumAttr = enumAttrs.FirstOrDefault(attr => (attr.ResourceUsage & resourceUsage) != 0); |
| | 88 | 295 | | if (enumAttr != null) |
| | 72 | 296 | | return enumAttr.ResourcePath; |
| | 24 | 297 | | } |
| | 40 | 298 | | } |
| | | 299 | | |
| | | 300 | | // 4. Fallback default |
| | 48 | 301 | | return "Properties.Resources"; |
| | 116 | 302 | | } |
| | | 303 | | |
| | | 304 | | /// <summary> |
| | | 305 | | /// Retrieves the file extension associated with a property, if specified. |
| | | 306 | | /// </summary> |
| | | 307 | | /// <param name="context"> |
| | | 308 | | /// The type descriptor context, which provides metadata about the property and its container. |
| | | 309 | | /// </param> |
| | | 310 | | /// <returns> |
| | | 311 | | /// A string containing the file extension for the resource. If no valid extension is found, returns an empty |
| | | 312 | | /// string. |
| | | 313 | | /// </returns> |
| | | 314 | | /// <remarks> |
| | | 315 | | /// This method determines the file extension based on the following order of precedence: |
| | | 316 | | /// <list type="number"> |
| | | 317 | | /// <item>Checks if the property has a <see cref="FileExtensionAttribute"/> and retrieves the |
| | | 318 | | /// value of the property it references.</item> |
| | | 319 | | /// <item>If the referenced property is a string, its value is returned.</item> |
| | | 320 | | /// <item>If the referenced property is an enumeration: |
| | | 321 | | /// <list type="bullet"> |
| | | 322 | | /// <item>Returns the enum's string representation, unless it is <c>None</c>, in which case an empty string is |
| | | 323 | | /// returned.</item> |
| | | 324 | | /// <item>If the enum field has an <see cref="EnumTextAttribute"/>, returns its custom text value.</item> |
| | | 325 | | /// <item>If the enum field has a <see cref="LocalizedEnumTextAttribute"/>, returns its localized |
| | | 326 | | /// text value.</item> |
| | | 327 | | /// </list> |
| | | 328 | | /// </item> |
| | | 329 | | /// <item>If no matching attributes are found, the method returns an empty string.</item> |
| | | 330 | | /// </list> |
| | | 331 | | /// |
| | | 332 | | /// Normally a user would not call this method directly, but it is used by the UIEditors |
| | | 333 | | /// to load values into the <see cref="PropertyGrid"/>. |
| | | 334 | | /// </remarks> |
| | | 335 | | /// <exception cref="InvalidOperationException"> |
| | | 336 | | /// Thrown if the referenced property is not found or is not public. |
| | | 337 | | /// </exception> |
| | | 338 | | /// <example> |
| | | 339 | | /// Example usage: |
| | | 340 | | /// <code> |
| | | 341 | | /// [FileExtension(nameof(FileType))] public string FileName { get; set; } = "example"; |
| | | 342 | | /// public string FileType { get; set; } = "xml"; |
| | | 343 | | /// |
| | | 344 | | /// var PropertyDescriptor = TypeDescriptor.GetProperties(this)[nameof(FileName)]; |
| | | 345 | | /// var context = new CustomTypeDescriptorContext(PropertyDescriptor, this); |
| | | 346 | | /// string extension = GetFileExtension(context); |
| | | 347 | | /// Console.WriteLine(extension); // Outputs: "xml" |
| | | 348 | | /// </code> |
| | | 349 | | /// </example> |
| | | 350 | | public static string GetFileExtension(ITypeDescriptorContext context) |
| | 52 | 351 | | { |
| | 60 | 352 | | if (context != null) |
| | 48 | 353 | | { |
| | 56 | 354 | | var FileExtensionAttr = FileExtensionAttribute.Get(context); |
| | 56 | 355 | | if (FileExtensionAttr != null) |
| | 48 | 356 | | { |
| | | 357 | | // Find the referenced property |
| | 56 | 358 | | var fileExtensionProperty = GetRequiredProperty(context.Instance, FileExtensionAttr.PropertyName); |
| | 48 | 359 | | if (fileExtensionProperty.PropertyType == typeof(string)) |
| | 12 | 360 | | { |
| | | 361 | | // Return the value of the referenced property |
| | 20 | 362 | | return fileExtensionProperty.GetValue(context.Instance, null) as string; |
| | | 363 | | } |
| | 44 | 364 | | else if (fileExtensionProperty.PropertyType.IsEnum || |
| | 44 | 365 | | (Nullable.GetUnderlyingType(fileExtensionProperty.PropertyType) is Type underlyingType && |
| | 44 | 366 | | underlyingType.IsEnum)) |
| | 32 | 367 | | { |
| | | 368 | | // Get the property value. |
| | 40 | 369 | | var rawValue = fileExtensionProperty.GetValue(context.Instance, null); |
| | 40 | 370 | | if (rawValue == null) |
| | | 371 | | // If the value is null, return an empty string (or handle as needed). |
| | 20 | 372 | | return string.Empty; |
| | | 373 | | |
| | | 374 | | // At this point rawValue should be an enum value. |
| | 36 | 375 | | var extension = (Enum)rawValue; |
| | 36 | 376 | | var enumField = extension.GetType().GetField(extension.ToString()); |
| | 36 | 377 | | if (enumField != null) |
| | 28 | 378 | | { |
| | 36 | 379 | | var enumTextAttr = enumField.GetCustomAttributes(typeof(EnumTextAttribute), false) as EnumTe |
| | 36 | 380 | | if (enumTextAttr.Length > 0) |
| | 24 | 381 | | return enumTextAttr[0].EnumText; // Return custom text |
| | 20 | 382 | | } |
| | | 383 | | // Return the value of the referenced property |
| | 28 | 384 | | return (string.IsNullOrEmpty(extension.ToString()) || |
| | 28 | 385 | | string.Equals(extension.ToString(), "None", StringComparison.OrdinalIgnoreCase)) |
| | 28 | 386 | | ? string.Empty |
| | 28 | 387 | | : extension.ToString(); |
| | | 388 | | } |
| | 12 | 389 | | } |
| | 12 | 390 | | } |
| | | 391 | | |
| | 24 | 392 | | return string.Empty; |
| | 44 | 393 | | } |
| | | 394 | | |
| | | 395 | | /// <summary> |
| | | 396 | | /// Retrieves a required public instance property from the specified object by name. |
| | | 397 | | /// </summary> |
| | | 398 | | /// <param name="instance"> |
| | | 399 | | /// The object instance whose property should be retrieved. Must not be <c>null</c>. |
| | | 400 | | /// </param> |
| | | 401 | | /// <param name="propertyName"> |
| | | 402 | | /// The name of the property to look up. Case-sensitive. |
| | | 403 | | /// </param> |
| | | 404 | | /// <returns> |
| | | 405 | | /// The <see cref="PropertyInfo"/> representing the requested public property if it exists. |
| | | 406 | | /// </returns> |
| | | 407 | | /// <exception cref="InvalidOperationException"> |
| | | 408 | | /// Thrown if the specified property does not exist on the type of <paramref name="instance"/>, |
| | | 409 | | /// or if the property exists but is not publicly accessible. |
| | | 410 | | /// </exception> |
| | | 411 | | /// <remarks> |
| | | 412 | | /// This method uses reflection to locate a property by name, including non-public declarations, |
| | | 413 | | /// but enforces that the property has a public getter. |
| | | 414 | | /// If the property cannot be found or fails the public visibility requirement, |
| | | 415 | | /// an <see cref="InvalidOperationException"/> is thrown. |
| | | 416 | | /// </remarks> |
| | | 417 | | private static PropertyInfo GetRequiredProperty(object instance, string propertyName) |
| | 48 | 418 | | { |
| | 56 | 419 | | var property = instance.GetType() |
| | 56 | 420 | | .GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) ?? |
| | 56 | 421 | | throw new InvalidOperationException( |
| | 56 | 422 | | $"Property '{propertyName}' not found on type '{instance.GetType()}'."); |
| | | 423 | | |
| | | 424 | | #if NET35 |
| | | 425 | | if (property.GetGetMethod() == null) |
| | | 426 | | #else |
| | 52 | 427 | | if (!property.GetMethod.IsPublic) |
| | | 428 | | #endif |
| | | 429 | | |
| | 12 | 430 | | { |
| | 20 | 431 | | throw new InvalidOperationException( |
| | 20 | 432 | | $"Property '{propertyName}' on type '{instance.GetType()}' must be public."); |
| | | 433 | | } |
| | | 434 | | |
| | 48 | 435 | | return property; |
| | 40 | 436 | | } |
| | | 437 | | |
| | | 438 | | #if NET35 |
| | | 439 | | /// <summary> |
| | | 440 | | /// Retrieves the first custom attribute of the specified type applied to the given <see cref="MemberInfo"/>. |
| | | 441 | | /// </summary> |
| | | 442 | | /// <typeparam name="T"> |
| | | 443 | | /// The type of attribute to retrieve. Must derive from <see cref="Attribute"/>. |
| | | 444 | | /// </typeparam> |
| | | 445 | | /// <param name="member"> |
| | | 446 | | /// The member (e.g., property, method, field, type) to inspect for the attribute. |
| | | 447 | | /// </param> |
| | | 448 | | /// <returns> |
| | | 449 | | /// An instance of the attribute type <typeparamref name="T"/> if one is defined on the member; otherwise, <c>nu |
| | | 450 | | /// </returns> |
| | | 451 | | /// <remarks> |
| | | 452 | | /// this uses <see cref="MemberInfo.GetCustomAttributes(Type, bool)"/> internally. |
| | | 453 | | /// </remarks> |
| | | 454 | | /// <exception cref="ArgumentNullException"> |
| | | 455 | | /// Thrown if <paramref name="member"/> is <c>null</c>. |
| | | 456 | | /// </exception> |
| | | 457 | | /// <example> |
| | | 458 | | /// Example usage: |
| | | 459 | | /// <code> |
| | | 460 | | /// var property = typeof(MyClass).GetProperty("MyProperty"); |
| | | 461 | | /// var attr = Support.GetFirstCustomAttribute<AutoCompleteSetupAttribute>(property); |
| | | 462 | | /// if (attr != null) |
| | | 463 | | /// { |
| | | 464 | | /// // Attribute was found |
| | | 465 | | /// } |
| | | 466 | | /// </code> |
| | | 467 | | /// </example> |
| | | 468 | | #else |
| | | 469 | | /// <summary> |
| | | 470 | | /// Retrieves the first custom attribute of the specified type applied to the given <see cref="MemberInfo"/>. |
| | | 471 | | /// </summary> |
| | | 472 | | /// <typeparam name="T"> |
| | | 473 | | /// The type of attribute to retrieve. Must derive from <see cref="Attribute"/>. |
| | | 474 | | /// </typeparam> |
| | | 475 | | /// <param name="member"> |
| | | 476 | | /// The member (e.g., property, method, field, type) to inspect for the attribute. |
| | | 477 | | /// </param> |
| | | 478 | | /// <returns> |
| | | 479 | | /// An instance of the attribute type <typeparamref name="T"/> if one is defined on the member; otherwise, <c>nu |
| | | 480 | | /// </returns> |
| | | 481 | | /// <remarks> |
| | | 482 | | /// On .NET Framework 3.5, this uses <see cref="MemberInfo.GetCustomAttributes(Type, bool)"/> internally; |
| | | 483 | | /// on later versions, it uses <see cref="CustomAttributeExtensions.GetCustomAttribute{T}(MemberInfo, bool)"/>. |
| | | 484 | | /// </remarks> |
| | | 485 | | /// <exception cref="ArgumentNullException"> |
| | | 486 | | /// Thrown if <paramref name="member"/> is <c>null</c>. |
| | | 487 | | /// </exception> |
| | | 488 | | /// <example> |
| | | 489 | | /// Example usage: |
| | | 490 | | /// <code> |
| | | 491 | | /// var property = typeof(MyClass).GetProperty("MyProperty"); |
| | | 492 | | /// var attr = Support.GetFirstCustomAttribute<AutoCompleteSetupAttribute>(property); |
| | | 493 | | /// if (attr != null) |
| | | 494 | | /// { |
| | | 495 | | /// // Attribute was found |
| | | 496 | | /// } |
| | | 497 | | /// </code> |
| | | 498 | | /// </example> |
| | | 499 | | #endif |
| | | 500 | | public static T GetFirstCustomAttribute<T>(MemberInfo member) where T : Attribute |
| | 748 | 501 | | { |
| | | 502 | | #if NET5_0_OR_GREATER |
| | 740 | 503 | | ArgumentNullException.ThrowIfNull(member); |
| | | 504 | | #else |
| | 16 | 505 | | if (member == null) |
| | 16 | 506 | | throw new ArgumentNullException(nameof(member)); |
| | | 507 | | #endif |
| | | 508 | | #if NET35 |
| | | 509 | | var attrs = member.GetCustomAttributes(typeof(T), true); |
| | | 510 | | return attrs.Length > 0 ? (T)attrs[0] : null; |
| | | 511 | | #else |
| | 752 | 512 | | return member.GetCustomAttribute<T>(true); |
| | | 513 | | #endif |
| | 744 | 514 | | } |
| | | 515 | | |
| | | 516 | | /// <summary> |
| | | 517 | | /// Retrieves the <see cref="FieldInfo"/> for a specific enum value. |
| | | 518 | | /// </summary> |
| | | 519 | | /// <param name="value">The enum value to inspect.</param> |
| | | 520 | | /// <returns> |
| | | 521 | | /// The <see cref="FieldInfo"/> corresponding to the specified enum value, or <c>null</c> |
| | | 522 | | /// if the value does not match a defined member of the enum type. |
| | | 523 | | /// </returns> |
| | | 524 | | /// <exception cref="ArgumentNullException"> |
| | | 525 | | /// Thrown when <paramref name="value"/> is <c>null</c>. |
| | | 526 | | /// </exception> |
| | | 527 | | public static FieldInfo GetEnumField(Enum value) |
| | 576 | 528 | | { |
| | | 529 | | #if NET5_0_OR_GREATER |
| | 568 | 530 | | ArgumentNullException.ThrowIfNull(value); |
| | | 531 | | #else |
| | 16 | 532 | | if (value == null) |
| | 16 | 533 | | throw new ArgumentNullException(nameof(value)); |
| | | 534 | | #endif |
| | | 535 | | |
| | 580 | 536 | | var type = value.GetType(); |
| | 580 | 537 | | var name = Enum.GetName(type, value); |
| | 580 | 538 | | return name == null ? null : type.GetField(name); |
| | 572 | 539 | | } |
| | | 540 | | |
| | | 541 | | /// <summary> |
| | | 542 | | /// Retrieves the <see cref="PropertyInfo"/> for the property described by the specified <see |
| | | 543 | | /// cref="ITypeDescriptorContext"/>. |
| | | 544 | | /// </summary> |
| | | 545 | | /// <param name="context"> |
| | | 546 | | /// The type descriptor context containing metadata about the property, including its name and declaring |
| | | 547 | | /// component type. |
| | | 548 | | /// </param> |
| | | 549 | | /// <returns> |
| | | 550 | | /// A <see cref="PropertyInfo"/> object representing the property described by <paramref name="context"/>. |
| | | 551 | | /// </returns> |
| | | 552 | | /// <exception cref="ArgumentNullException"> |
| | | 553 | | /// Thrown when <paramref name="context"/> is <c>null</c>. |
| | | 554 | | /// </exception> |
| | | 555 | | /// <exception cref="ArgumentException"> |
| | | 556 | | /// Thrown when <paramref name="context"/> does not contain a valid <see cref="PropertyDescriptor"/>. |
| | | 557 | | /// </exception> |
| | | 558 | | /// <exception cref="InvalidOperationException"> |
| | | 559 | | /// Thrown when the property described by the context could not be found on the component type. |
| | | 560 | | /// </exception> |
| | | 561 | | /// <example> |
| | | 562 | | /// Example usage: <code> var context = TypeDescriptorContext.Create(typeof(MyClass), "MyProperty"); |
| | | 563 | | /// PropertyInfo info = Support.GetPropertyInfo(context);</code> |
| | | 564 | | /// </example> |
| | | 565 | | public static PropertyInfo GetPropertyInfo(ITypeDescriptorContext context) |
| | 256 | 566 | | { |
| | | 567 | | #if NET5_0_OR_GREATER |
| | 248 | 568 | | ArgumentNullException.ThrowIfNull(context); |
| | | 569 | | #else |
| | 16 | 570 | | if (context == null) |
| | 16 | 571 | | throw new ArgumentNullException(nameof(context)); |
| | | 572 | | #endif |
| | 260 | 573 | | if (context.PropertyDescriptor == null) |
| | 20 | 574 | | throw new ArgumentException("Context does not contain a valid PropertyDescriptor.", nameof(context)); |
| | | 575 | | |
| | 256 | 576 | | var componentType = context.PropertyDescriptor.ComponentType; |
| | 256 | 577 | | var propertyName = context.PropertyDescriptor.Name; |
| | 256 | 578 | | var propInfo = componentType.GetProperty(propertyName) ?? |
| | 256 | 579 | | throw new InvalidOperationException( |
| | 256 | 580 | | $"Property '{propertyName}' not found on type {componentType.FullName}."); |
| | 248 | 581 | | return propInfo; |
| | 240 | 582 | | } |
| | | 583 | | |
| | | 584 | | /// <summary> |
| | | 585 | | /// Sets the current thread's culture and UI culture to the specified language identifier. |
| | | 586 | | /// </summary> |
| | | 587 | | /// <param name="language"> |
| | | 588 | | /// The language code (e.g., <c>"en-US"</c>, <c>"fr-FR"</c>, <c>"de-DE"</c>) to apply to |
| | | 589 | | /// <see cref="Thread.CurrentCulture"/> and <see cref="Thread.CurrentUICulture"/>. |
| | | 590 | | /// </param> |
| | | 591 | | /// <remarks> |
| | | 592 | | /// This method is intended primarily for testing and debugging localization or globalization scenarios, |
| | | 593 | | /// allowing the current thread to simulate running under a specific culture. |
| | | 594 | | /// </remarks> |
| | | 595 | | public static void SetLanguage(string language) |
| | 12 | 596 | | { |
| | 20 | 597 | | Thread.CurrentThread.CurrentCulture = new CultureInfo(language); |
| | 20 | 598 | | Thread.CurrentThread.CurrentUICulture = new CultureInfo(language); |
| | 20 | 599 | | } |
| | | 600 | | } |
| | | 601 | | } |