| | 1 | | using System; |
| | 2 | | using System.ComponentModel; |
| | 3 | |
|
| | 4 | | namespace PropertyGridHelpers.TypeDescriptors |
| | 5 | | { |
| | 6 | | #if NET8_0_OR_GREATER |
| | 7 | | /// <summary> |
| | 8 | | /// Provides a custom <see cref="ITypeDescriptorContext"/> implementation |
| | 9 | | /// for use in property editors, supporting design-time or test-time scenarios. |
| | 10 | | /// </summary> |
| | 11 | | /// <param name="propertyDescriptor"> |
| | 12 | | /// The <see cref="PropertyDescriptor"/> describing the property being edited. |
| | 13 | | /// </param> |
| | 14 | | /// <param name="instance"> |
| | 15 | | /// The object instance whose property is being edited. |
| | 16 | | /// </param> |
| | 17 | | /// <seealso cref="ITypeDescriptorContext"/> |
| 416 | 18 | | public partial class CustomTypeDescriptorContext( |
| 416 | 19 | | PropertyDescriptor propertyDescriptor, |
| 416 | 20 | | object instance) : ITypeDescriptorContext |
| | 21 | | { |
| | 22 | | #else |
| | 23 | | /// <summary> |
| | 24 | | /// Provides a custom <see cref="ITypeDescriptorContext"/> implementation |
| | 25 | | /// for use in property editors, supporting design-time or test-time scenarios. |
| | 26 | | /// </summary> |
| | 27 | | /// <seealso cref="ITypeDescriptorContext"/> |
| | 28 | | public partial class CustomTypeDescriptorContext : ITypeDescriptorContext |
| | 29 | | { |
| | 30 | | /// <summary> |
| | 31 | | /// Initializes a new instance of the <see cref="CustomTypeDescriptorContext"/> class. |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="propertyDescriptor"> |
| | 34 | | /// The <see cref="PropertyDescriptor"/> describing the property being edited. |
| | 35 | | /// </param> |
| | 36 | | /// <param name="instance"> |
| | 37 | | /// The object instance whose property is being edited. |
| | 38 | | /// </param> |
| 16 | 39 | | public CustomTypeDescriptorContext( |
| 16 | 40 | | PropertyDescriptor propertyDescriptor, |
| 16 | 41 | | object instance) |
| 8 | 42 | | { |
| 16 | 43 | | PropertyDescriptor = propertyDescriptor; |
| 16 | 44 | | Instance = instance; |
| 16 | 45 | | } |
| | 46 | | #endif |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Gets the container object associated with this context. Always returns <c>null</c> |
| | 50 | | /// since this implementation does not provide container services. |
| | 51 | | /// </summary> |
| 44 | 52 | | public IContainer Container => null; |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Gets the instance whose property is being edited. |
| | 56 | | /// </summary> |
| | 57 | | public object Instance |
| | 58 | | { |
| 732 | 59 | | get; |
| | 60 | | #if NET8_0_OR_GREATER |
| 416 | 61 | | } = instance; |
| | 62 | | #else |
| | 63 | | } |
| | 64 | | #endif |
| | 65 | | /// <summary> |
| | 66 | | /// Gets the <see cref="PropertyDescriptor"/> describing the property being edited. |
| | 67 | | /// </summary> |
| | 68 | | public PropertyDescriptor PropertyDescriptor |
| | 69 | | { |
| 1392 | 70 | | get; |
| | 71 | | #if NET8_0_OR_GREATER |
| 416 | 72 | | } = propertyDescriptor; |
| | 73 | | #else |
| | 74 | | } |
| | 75 | | #endif |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Gets a service object of the specified type. Always returns <c>null</c> |
| | 79 | | /// in this implementation, as no services are provided. |
| | 80 | | /// </summary> |
| | 81 | | /// <param name="serviceType">The type of service requested.</param> |
| | 82 | | /// <returns>Always <c>null</c>.</returns> |
| 44 | 83 | | public object GetService(Type serviceType) => null; // Provide any required services here. |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Called when a component has changed. This implementation does nothing. |
| | 87 | | /// </summary> |
| | 88 | | public void OnComponentChanged() |
| 36 | 89 | | { |
| 44 | 90 | | } |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Called before a component is changed. This implementation always returns <c>true</c>, |
| | 94 | | /// indicating that changes are permitted. |
| | 95 | | /// </summary> |
| | 96 | | /// <returns>Always <c>true</c>.</returns> |
| 44 | 97 | | public bool OnComponentChanging() => true; |
| | 98 | |
|
| | 99 | | /// <summary> |
| | 100 | | /// Creates a new <see cref="CustomTypeDescriptorContext"/> for the specified type and property name. |
| | 101 | | /// </summary> |
| | 102 | | /// <param name="type">The type whose property is to be described.</param> |
| | 103 | | /// <param name="propertyName">The name of the property to describe.</param> |
| | 104 | | /// <returns> |
| | 105 | | /// An instance of <see cref="CustomTypeDescriptorContext"/> representing the requested type and property. |
| | 106 | | /// </returns> |
| | 107 | | public static ITypeDescriptorContext Create(Type type, string propertyName) |
| 104 | 108 | | { |
| 112 | 109 | | var instance = type == null ? null : Activator.CreateInstance(type); |
| 112 | 110 | | var propertyDescriptor = type == null || string.IsNullOrEmpty(propertyName) ? |
| 112 | 111 | | null : TypeDescriptor.GetProperties(type)[propertyName]; |
| 112 | 112 | | return new CustomTypeDescriptorContext(propertyDescriptor, instance); |
| 104 | 113 | | } |
| | 114 | | } |
| | 115 | | } |