| | 1 | | using System; |
| | 2 | | using System.ComponentModel; |
| | 3 | |
|
| | 4 | | namespace PropertyGridHelpers.Attributes |
| | 5 | | { |
| | 6 | | #if NET8_0_OR_GREATER |
| | 7 | | /// <summary> |
| | 8 | | /// Specifies a property containing the file extension to use when resolving |
| | 9 | | /// resource names for an <see cref="UIEditors.ImageTextUIEditor"/>. |
| | 10 | | /// </summary> |
| | 11 | | /// <param name="propertyName"> |
| | 12 | | /// The name of the property on the same object that provides the file extension value. |
| | 13 | | /// </param> |
| | 14 | | /// <remarks> |
| | 15 | | /// This attribute is intended for scenarios where an image resource is dynamically |
| | 16 | | /// constructed from an enum value plus a file extension. It works together with |
| | 17 | | /// <see cref="ResourcePathAttribute"/>, <see cref="DynamicPathSourceAttribute"/>, and |
| | 18 | | /// <see cref="EnumImageAttribute"/> to build a complete resource path for images or icons. |
| | 19 | | /// </remarks> |
| | 20 | | /// <seealso cref="Attribute"/> |
| | 21 | | [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] |
| 68 | 22 | | public class FileExtensionAttribute(string propertyName) : Attribute |
| | 23 | | #else |
| | 24 | | /// <summary> |
| | 25 | | /// Specifies a property containing the file extension to use when resolving |
| | 26 | | /// resource names for an <see cref="UIEditors.ImageTextUIEditor"/>. |
| | 27 | | /// </summary> |
| | 28 | | /// <remarks> |
| | 29 | | /// This attribute is intended for scenarios where an image resource is dynamically |
| | 30 | | /// constructed from an enum value plus a file extension. It works together with |
| | 31 | | /// <see cref="ResourcePathAttribute"/>, <see cref="DynamicPathSourceAttribute"/>, and |
| | 32 | | /// <see cref="EnumImageAttribute"/> to build a complete resource path for images or icons. |
| | 33 | | /// </remarks> |
| | 34 | | /// <seealso cref="Attribute"/> |
| | 35 | | [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] |
| | 36 | | public class FileExtensionAttribute : Attribute |
| | 37 | | #endif |
| | 38 | | { |
| | 39 | | /// <summary> |
| | 40 | | /// Gets the name of the property containing the file extension. |
| | 41 | | /// </summary> |
| | 42 | | /// <value> |
| | 43 | | /// The name of the property on the target object that will be used to determine |
| | 44 | | /// the file extension when building the resource path. |
| | 45 | | /// </value> |
| | 46 | | public string PropertyName |
| | 47 | | { |
| 44 | 48 | | get; |
| | 49 | | #if NET8_0_OR_GREATER |
| 68 | 50 | | } = propertyName; |
| | 51 | | #else |
| | 52 | | } |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Gets the name of the property containing the file extension. |
| | 56 | | /// </summary> |
| | 57 | | /// <value> |
| | 58 | | /// The name of the property on the target object that will be used to determine |
| | 59 | | /// the file extension when building the resource path. |
| | 60 | | /// </value> |
| 16 | 61 | | public FileExtensionAttribute(string propertyName) => PropertyName = propertyName; |
| | 62 | | #endif |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Checks whether a <see cref="FileExtensionAttribute"/> exists on the current |
| | 66 | | /// property within the given context. |
| | 67 | | /// </summary> |
| | 68 | | /// <param name="context">The type descriptor context.</param> |
| | 69 | | /// <returns> |
| | 70 | | /// <c>true</c> if the attribute is applied; otherwise, <c>false</c>. |
| | 71 | | /// </returns> |
| | 72 | | public static bool Exists(ITypeDescriptorContext context) => |
| 24 | 73 | | Get(context) != null; |
| | 74 | |
|
| | 75 | | /// <summary> |
| | 76 | | /// Retrieves the <see cref="FileExtensionAttribute"/> applied to the property |
| | 77 | | /// described by the given context, if any. |
| | 78 | | /// </summary> |
| | 79 | | /// <param name="context">The type descriptor context.</param> |
| | 80 | | /// <returns> |
| | 81 | | /// The <see cref="FileExtensionAttribute"/>, or <c>null</c> if none is applied. |
| | 82 | | /// </returns> |
| | 83 | | public static FileExtensionAttribute Get(ITypeDescriptorContext context) => |
| 68 | 84 | | context == null || context.Instance == null || context.PropertyDescriptor == null |
| 68 | 85 | | ? null |
| 68 | 86 | | : Support.Support.GetFirstCustomAttribute<FileExtensionAttribute>( |
| 68 | 87 | | Support.Support.GetPropertyInfo(context)); |
| | 88 | | } |
| | 89 | | } |