ReflectionPropertyDescriptor.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // System.ComponentModel.PropertyDescriptor.cs
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) Novell, Inc.
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Reflection;
  32. using System.Runtime.InteropServices;
  33. using System.ComponentModel.Design;
  34. namespace System.ComponentModel
  35. {
  36. internal class ReflectionPropertyDescriptor : PropertyDescriptor
  37. {
  38. PropertyInfo _member;
  39. Type _componentType;
  40. public ReflectionPropertyDescriptor (Type componentType, PropertyDescriptor oldPropertyDescriptor, Attribute [] attributes)
  41. : base (oldPropertyDescriptor, attributes)
  42. {
  43. _componentType = componentType;
  44. }
  45. public ReflectionPropertyDescriptor (Type componentType, string name, Type type, Attribute [] attributes)
  46. : base (name, attributes)
  47. {
  48. _componentType = componentType;
  49. }
  50. public ReflectionPropertyDescriptor (PropertyInfo info)
  51. : base (info.Name, (Attribute[])info.GetCustomAttributes (true))
  52. {
  53. _member = info;
  54. _componentType = _member.DeclaringType;
  55. }
  56. PropertyInfo GetPropertyInfo ()
  57. {
  58. if (_member == null) {
  59. _member = _componentType.GetProperty (Name);
  60. if (_member == null)
  61. throw new ArgumentException ("Accessor methods for the " + Name + " property are missing");
  62. }
  63. return _member;
  64. }
  65. public override Type ComponentType
  66. {
  67. get { return _componentType; }
  68. }
  69. public override bool IsReadOnly
  70. {
  71. get
  72. {
  73. return !GetPropertyInfo ().CanWrite;
  74. }
  75. }
  76. public override Type PropertyType
  77. {
  78. get
  79. {
  80. return GetPropertyInfo ().PropertyType;
  81. }
  82. }
  83. public override object GetValue (object component)
  84. {
  85. return GetPropertyInfo ().GetValue (component, null);
  86. }
  87. DesignerTransaction CreateTransaction (object obj)
  88. {
  89. IComponent com = obj as IComponent;
  90. if (com == null || com.Site == null) return null;
  91. IDesignerHost dh = (IDesignerHost) com.Site.GetService (typeof(IDesignerHost));
  92. if (dh == null) return null;
  93. DesignerTransaction tran = dh.CreateTransaction ();
  94. IComponentChangeService ccs = (IComponentChangeService) com.Site.GetService (typeof(IComponentChangeService));
  95. if (ccs != null)
  96. ccs.OnComponentChanging (com, this);
  97. return tran;
  98. }
  99. void EndTransaction (object obj, DesignerTransaction tran, object oldValue, object newValue, bool commit)
  100. {
  101. if (tran == null) return;
  102. if (commit) {
  103. Component com = obj as Component;
  104. IComponentChangeService ccs = (IComponentChangeService) com.Site.GetService (typeof(IComponentChangeService));
  105. if (ccs != null)
  106. ccs.OnComponentChanged (com, this, oldValue, newValue);
  107. tran.Commit ();
  108. }
  109. else
  110. tran.Cancel ();
  111. }
  112. public override void SetValue (object component, object value)
  113. {
  114. DesignerTransaction tran = CreateTransaction (component);
  115. object old = GetValue (component);
  116. try
  117. {
  118. GetPropertyInfo ().SetValue (component, value, null);
  119. EndTransaction (component, tran, old, value, true);
  120. }
  121. catch
  122. {
  123. EndTransaction (component, tran, old, value, false);
  124. throw;
  125. }
  126. }
  127. public override void ResetValue (object component)
  128. {
  129. DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
  130. if (attrib != null) {
  131. SetValue (component, attrib.Value);
  132. }
  133. DesignerTransaction tran = CreateTransaction (component);
  134. object old = GetValue (component);
  135. try
  136. {
  137. MethodInfo mi = component.GetType().GetMethod ("Reset" + Name, Type.EmptyTypes);
  138. if (mi != null) mi.Invoke (component, null);
  139. EndTransaction (component, tran, old, GetValue (component), true);
  140. }
  141. catch
  142. {
  143. EndTransaction (component, tran, old, GetValue (component), false);
  144. throw;
  145. }
  146. }
  147. public override bool CanResetValue (object component)
  148. {
  149. DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
  150. if (attrib != null) {
  151. object current = GetValue (component);
  152. if ((attrib.Value == null || current == null) && attrib.Value != current) return true;
  153. return !attrib.Value.Equals (current);
  154. }
  155. else {
  156. MethodInfo mi = component.GetType().GetMethod ("ShouldPersist" + Name, Type.EmptyTypes);
  157. if (mi != null) return (bool) mi.Invoke (component, null);
  158. mi = component.GetType().GetMethod ("Reset" + Name, Type.EmptyTypes);
  159. return (mi != null);
  160. }
  161. }
  162. public override bool ShouldSerializeValue (object component)
  163. {
  164. DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
  165. if (attrib != null) {
  166. object current = GetValue (component);
  167. if ((attrib.Value == null || current == null) && attrib.Value != current) return true;
  168. return !attrib.Value.Equals (current);
  169. }
  170. else {
  171. MethodInfo mi = component.GetType().GetMethod ("ShouldSerialize" + Name, Type.EmptyTypes);
  172. if (mi != null) return (bool) mi.Invoke (component, null);
  173. return true;
  174. }
  175. }
  176. }
  177. }