| | 1 | | using PropertyGridHelpers.TypeDescriptors; |
| | 2 | | using System; |
| | 3 | | using System.ComponentModel; |
| | 4 | |
|
| | 5 | | namespace PropertyGridHelpers.TypeDescriptionProviders |
| | 6 | | { |
| | 7 | | #if NET8_0_OR_GREATER |
| | 8 | | /// <summary> |
| | 9 | | /// Localized Type Description Provider |
| | 10 | | /// </summary> |
| | 11 | | /// <param name="type">The type.</param> |
| | 12 | | /// <seealso cref="TypeDescriptionProvider" /> |
| | 13 | | /// <remarks> |
| | 14 | | /// Initializes a new instance of the <see cref="LocalizedTypeDescriptionProvider"/> class. |
| | 15 | | /// </remarks> |
| | 16 | | /// <example> |
| | 17 | | /// <code language="csharp"> |
| | 18 | | /// [ResourcePath(nameof(TestControl))] |
| | 19 | | /// [TypeDescriptionProvider(typeof(LocalizedTypeDescriptionProvider))] |
| | 20 | | /// public partial class TestControl : UserControl |
| | 21 | | /// { |
| | 22 | | /// [LocalizedCategory("Category_Layout")] |
| | 23 | | /// [LocalizedDescription("Description_Scrollbar")] |
| | 24 | | /// [LocalizedDisplayName("DisplayName_Scrollbar")] |
| | 25 | | /// [Editor(typeof(FlagEnumUIEditor<EnumTextConverter<ScrollBars>>), typeof(UITypeEditor))] |
| | 26 | | /// [TypeConverter(typeof(EnumTextConverter<ScrollBars>))] |
| | 27 | | /// [DefaultValue(ScrollBars.None)] |
| | 28 | | /// [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] |
| | 29 | | /// [EditorBrowsable(EditorBrowsableState.Always)] |
| | 30 | | /// [Bindable(true)] |
| | 31 | | /// public ScrollBars Scrollbars |
| | 32 | | /// { |
| | 33 | | /// get => _Scrollbars; |
| | 34 | | /// set => _Scrollbars = value; |
| | 35 | | /// } |
| | 36 | | /// } |
| | 37 | | /// </code> |
| | 38 | | /// </example> |
| | 39 | | public class LocalizedTypeDescriptionProvider(Type type) : |
| 16 | 40 | | TypeDescriptionProvider(TypeDescriptor.GetProvider(type)) |
| | 41 | | { |
| 16 | 42 | | private readonly TypeDescriptionProvider baseProvider = TypeDescriptor.GetProvider(type); |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Initializes a new instance of the <see cref="LocalizedTypeDescriptionProvider"/> class. |
| | 46 | | /// </summary> |
| | 47 | | public LocalizedTypeDescriptionProvider() |
| 12 | 48 | | : this(typeof(object)) { } // Provide a fallback for dynamic creation |
| | 49 | | #else |
| | 50 | | /// <summary> |
| | 51 | | /// Localized Type Description Provider |
| | 52 | | /// </summary> |
| | 53 | | /// <seealso cref="TypeDescriptionProvider" /> |
| | 54 | | /// <example> |
| | 55 | | /// <code language="csharp"> |
| | 56 | | /// [ResourcePath(nameof(TestControl))] |
| | 57 | | /// [TypeDescriptionProvider(typeof(LocalizedTypeDescriptionProvider))] |
| | 58 | | /// public partial class TestControl : UserControl |
| | 59 | | /// { |
| | 60 | | /// [LocalizedCategory("Category_Layout")] |
| | 61 | | /// [LocalizedDescription("Description_Scrollbar")] |
| | 62 | | /// [LocalizedDisplayName("DisplayName_Scrollbar")] |
| | 63 | | /// [Editor(typeof(FlagEnumUIEditor<EnumTextConverter<ScrollBars>>), typeof(UITypeEditor))] |
| | 64 | | /// [TypeConverter(typeof(EnumTextConverter<ScrollBars>))] |
| | 65 | | /// [DefaultValue(ScrollBars.None)] |
| | 66 | | /// [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] |
| | 67 | | /// [EditorBrowsable(EditorBrowsableState.Always)] |
| | 68 | | /// [Bindable(true)] |
| | 69 | | /// public ScrollBars Scrollbars |
| | 70 | | /// { |
| | 71 | | /// get => _Scrollbars; |
| | 72 | | /// set => _Scrollbars = value; |
| | 73 | | /// } |
| | 74 | | /// } |
| | 75 | | /// </code> |
| | 76 | | /// </example> |
| | 77 | | public class LocalizedTypeDescriptionProvider : |
| | 78 | | TypeDescriptionProvider |
| | 79 | | { |
| | 80 | | private readonly TypeDescriptionProvider baseProvider; |
| | 81 | |
|
| | 82 | | /// <summary> |
| | 83 | | /// Initializes a new instance of the <see cref="LocalizedTypeDescriptionProvider"/> class. |
| | 84 | | /// </summary> |
| | 85 | | /// <param name="type">The type.</param> |
| | 86 | | public LocalizedTypeDescriptionProvider(Type type) |
| 16 | 87 | | : base(TypeDescriptor.GetProvider(type)) => |
| 16 | 88 | | baseProvider = TypeDescriptor.GetProvider(type); |
| | 89 | |
|
| | 90 | | /// <summary> |
| | 91 | | /// Initializes a new instance of the <see cref="LocalizedTypeDescriptionProvider"/> class. |
| | 92 | | /// </summary> |
| | 93 | | public LocalizedTypeDescriptionProvider() |
| 16 | 94 | | : this(typeof(object)) { } // Provide a fallback for dynamic creation |
| | 95 | | #endif |
| | 96 | |
|
| | 97 | | /// <summary> |
| | 98 | | /// Returns a custom type descriptor for the specified type and instance, enabling localization or |
| | 99 | | /// other metadata customization. |
| | 100 | | /// </summary> |
| | 101 | | /// <param name="objectType"> |
| | 102 | | /// The <see cref="Type"/> of the object for which to retrieve the type descriptor. |
| | 103 | | /// Cannot be <c>null</c>. |
| | 104 | | /// </param> |
| | 105 | | /// <param name="instance"> |
| | 106 | | /// An optional instance of the object whose type descriptor is being requested. May be <c>null</c>. |
| | 107 | | /// </param> |
| | 108 | | /// <returns> |
| | 109 | | /// A custom <see cref="ICustomTypeDescriptor"/> for the specified type, wrapping the base descriptor |
| | 110 | | /// to provide additional metadata support. |
| | 111 | | /// </returns> |
| | 112 | | /// <exception cref="ArgumentNullException"> |
| | 113 | | /// Thrown if <paramref name="objectType"/> is <c>null</c>. |
| | 114 | | /// </exception> |
| | 115 | | /// <remarks> |
| | 116 | | /// This override decorates the standard type descriptor returned by the base provider |
| | 117 | | /// with a <see cref="LocalizedTypeDescriptor"/> to enable localization of property and event metadata. |
| | 118 | | /// </remarks> |
| | 119 | | public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance) |
| 16 | 120 | | { |
| | 121 | | #if NET8_0_OR_GREATER |
| 8 | 122 | | ArgumentNullException.ThrowIfNull(objectType); |
| | 123 | | #else |
| 16 | 124 | | if (objectType == null) |
| 16 | 125 | | throw new ArgumentNullException(nameof(objectType)); |
| | 126 | | #endif |
| | 127 | |
|
| 20 | 128 | | var baseDescriptor = baseProvider.GetTypeDescriptor(objectType, instance); |
| 20 | 129 | | return new LocalizedTypeDescriptor(baseDescriptor); |
| 12 | 130 | | } |
| | 131 | | } |
| | 132 | | } |