Guest User

Untitled

a guest
Mar 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. public class ReadOnlyTypeDescriptor : ICustomTypeDescriptor
  2. {
  3. public ReadOnlyTypeDescriptor(object target)
  4. {
  5. TypeDescriptionProvider provider = TypeDescriptor.GetProvider(target);
  6. _originalDescriptor = provider.GetTypeDescriptor(target);
  7. }
  8.  
  9. public ReadOnlyTypeDescriptor(ICustomTypeDescriptor descriptor)
  10. {
  11. _originalDescriptor = descriptor;
  12. }
  13.  
  14. private ICustomTypeDescriptor _originalDescriptor;
  15.  
  16. private PropertyDescriptor MakeReadOnly(PropertyDescriptor propertyDescriptor)
  17. {
  18. return new ReadOnlyPropertyDescriptor(propertyDescriptor);
  19. }
  20.  
  21. private PropertyDescriptorCollection MakeReadOnly(PropertyDescriptorCollection propertyDescriptors)
  22. {
  23. var descriptors = propertyDescriptors
  24. .Cast<PropertyDescriptor>()
  25. .Select(pd => new ReadOnlyPropertyDescriptor(pd))
  26. .ToArray();
  27. return new PropertyDescriptorCollection(descriptors, true);
  28. }
  29.  
  30. #region ICustomTypeDescriptor Members
  31.  
  32. public AttributeCollection GetAttributes()
  33. {
  34. return _originalDescriptor.GetAttributes();
  35. }
  36.  
  37. public string GetClassName()
  38. {
  39. return _originalDescriptor.GetClassName();
  40. }
  41.  
  42. public string GetComponentName()
  43. {
  44. return _originalDescriptor.GetComponentName();
  45. }
  46.  
  47. public TypeConverter GetConverter()
  48. {
  49. return _originalDescriptor.GetConverter();
  50. }
  51.  
  52. public EventDescriptor GetDefaultEvent()
  53. {
  54. return _originalDescriptor.GetDefaultEvent();
  55. }
  56.  
  57. public PropertyDescriptor GetDefaultProperty()
  58. {
  59. return MakeReadOnly(_originalDescriptor.GetDefaultProperty());
  60. }
  61.  
  62. public object GetEditor(Type editorBaseType)
  63. {
  64. return _originalDescriptor.GetEditor(editorBaseType);
  65. }
  66.  
  67. public EventDescriptorCollection GetEvents(Attribute[] attributes)
  68. {
  69. return _originalDescriptor.GetEvents(attributes);
  70. }
  71.  
  72. public EventDescriptorCollection GetEvents()
  73. {
  74. return _originalDescriptor.GetEvents();
  75. }
  76.  
  77. public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
  78. {
  79. return MakeReadOnly(_originalDescriptor.GetProperties(attributes));
  80. }
  81.  
  82. public PropertyDescriptorCollection GetProperties()
  83. {
  84. return MakeReadOnly(_originalDescriptor.GetProperties());
  85. }
  86.  
  87. public object GetPropertyOwner(PropertyDescriptor pd)
  88. {
  89. return _originalDescriptor.GetPropertyOwner(pd);
  90. }
  91.  
  92. #endregion
  93. }
  94.  
  95. public class ReadOnlyPropertyDescriptor : PropertyDescriptor
  96. {
  97. public ReadOnlyPropertyDescriptor(PropertyDescriptor descriptor)
  98. : base(
  99. descriptor.Name,
  100. descriptor.Attributes.Cast<Attribute>().ToArray())
  101. {
  102. _originalDescriptor = descriptor;
  103. }
  104.  
  105. private PropertyDescriptor _originalDescriptor;
  106.  
  107. public override bool CanResetValue(object component)
  108. {
  109. return false;
  110. }
  111.  
  112. public override Type ComponentType
  113. {
  114. get { return _originalDescriptor.ComponentType; }
  115. }
  116.  
  117. public override object GetValue(object component)
  118. {
  119. return _originalDescriptor.GetValue(component);
  120. }
  121.  
  122. public override bool IsReadOnly
  123. {
  124. get { return true; }
  125. }
  126.  
  127. public override Type PropertyType
  128. {
  129. get { return _originalDescriptor.PropertyType; }
  130. }
  131.  
  132. public override void ResetValue(object component)
  133. {
  134. throw new NotSupportedException();
  135. }
  136.  
  137. public override void SetValue(object component, object value)
  138. {
  139. throw new NotSupportedException();
  140. }
  141.  
  142. public override bool ShouldSerializeValue(object component)
  143. {
  144. return _originalDescriptor.ShouldSerializeValue(component);
  145. }
  146. }
  147.  
  148. propertyGrid.SelectedObject = new ReadOnlyTypeDescriptor(target);
Add Comment
Please, Sign In to add comment