ReflectionPropertyDescriptor.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // System.ComponentModel.PropertyDescriptor.cs
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual ([email protected])
  6. // Ivan N. Zlatev (contact i-nZ.net)
  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, BindingFlags.GetProperty | BindingFlags.NonPublic |
  60. BindingFlags.Public | BindingFlags.Instance);
  61. if (_member == null)
  62. throw new ArgumentException ("Accessor methods for the " + Name + " property are missing");
  63. }
  64. return _member;
  65. }
  66. public override Type ComponentType {
  67. get { return _componentType; }
  68. }
  69. public override bool IsReadOnly {
  70. get {
  71. bool attr_ro = false;
  72. ReadOnlyAttribute attrib = ((ReadOnlyAttribute) Attributes[typeof (ReadOnlyAttribute)]);
  73. if (attrib != null)
  74. attr_ro = attrib.IsReadOnly;
  75. return !GetPropertyInfo ().CanWrite || attrib.IsReadOnly;
  76. }
  77. }
  78. public override Type PropertyType {
  79. get {
  80. return GetPropertyInfo ().PropertyType;
  81. }
  82. }
  83. public override object GetValue (object component)
  84. {
  85. component = MemberDescriptor.GetInvokee (_componentType, component);
  86. return GetPropertyInfo ().GetValue (component, null);
  87. }
  88. DesignerTransaction CreateTransaction (object obj)
  89. {
  90. IComponent com = obj as IComponent;
  91. if (com == null || com.Site == null)
  92. return null;
  93. IDesignerHost dh = (IDesignerHost) com.Site.GetService (typeof(IDesignerHost));
  94. if (dh == null)
  95. return null;
  96. DesignerTransaction tran = dh.CreateTransaction ();
  97. IComponentChangeService ccs = (IComponentChangeService) com.Site.GetService (typeof(IComponentChangeService));
  98. if (ccs != null)
  99. ccs.OnComponentChanging (com, this);
  100. return tran;
  101. }
  102. void EndTransaction (object obj, DesignerTransaction tran, object oldValue, object newValue, bool commit)
  103. {
  104. if (tran == null) {
  105. // FIXME: EventArgs might be differen type.
  106. OnValueChanged (obj, new PropertyChangedEventArgs (Name));
  107. return;
  108. }
  109. if (commit) {
  110. IComponent com = obj as IComponent;
  111. IComponentChangeService ccs = (IComponentChangeService) com.Site.GetService (typeof(IComponentChangeService));
  112. if (ccs != null)
  113. ccs.OnComponentChanged (com, this, oldValue, newValue);
  114. tran.Commit ();
  115. // FIXME: EventArgs might be differen type.
  116. OnValueChanged (obj, new PropertyChangedEventArgs (Name));
  117. } else
  118. tran.Cancel ();
  119. }
  120. public override void SetValue (object component, object value)
  121. {
  122. DesignerTransaction tran = CreateTransaction (component);
  123. object propertyHolder = MemberDescriptor.GetInvokee (_componentType, component);
  124. object old = GetValue (propertyHolder);
  125. try {
  126. GetPropertyInfo ().SetValue (propertyHolder, value, null);
  127. EndTransaction (component, tran, old, value, true);
  128. } catch {
  129. EndTransaction (component, tran, old, value, false);
  130. throw;
  131. }
  132. }
  133. MethodInfo FindPropertyMethod (object o, string method_name)
  134. {
  135. MethodInfo mi = null;
  136. string name = method_name + Name;
  137. foreach (MethodInfo m in o.GetType().GetMethods (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) {
  138. // XXX should we really not check the return type of the method?
  139. if (m.Name == name && m.GetParameters().Length == 0) {
  140. mi = m;
  141. break;
  142. }
  143. }
  144. return mi;
  145. }
  146. public override void ResetValue (object component)
  147. {
  148. object propertyHolder = MemberDescriptor.GetInvokee (_componentType, component);
  149. DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
  150. if (attrib != null)
  151. SetValue (propertyHolder, attrib.Value);
  152. DesignerTransaction tran = CreateTransaction (component);
  153. object old = GetValue (propertyHolder);
  154. try {
  155. MethodInfo mi = FindPropertyMethod (propertyHolder, "Reset");
  156. if (mi != null)
  157. mi.Invoke (propertyHolder, null);
  158. EndTransaction (component, tran, old, GetValue (propertyHolder), true);
  159. } catch {
  160. EndTransaction (component, tran, old, GetValue (propertyHolder), false);
  161. throw;
  162. }
  163. }
  164. public override bool CanResetValue (object component)
  165. {
  166. component = MemberDescriptor.GetInvokee (_componentType, component);
  167. DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
  168. if (attrib != null) {
  169. object current = GetValue (component);
  170. if (attrib.Value == null || current == null){
  171. if (attrib.Value != current)
  172. return true;
  173. if (attrib.Value == null && current == null)
  174. return false;
  175. }
  176. return !attrib.Value.Equals (current);
  177. } else {
  178. #if NET_2_0
  179. if (!_member.CanWrite)
  180. return false;
  181. #endif
  182. MethodInfo mi = FindPropertyMethod (component, "ShouldPersist");
  183. if (mi != null)
  184. return (bool) mi.Invoke (component, null);
  185. mi = FindPropertyMethod (component, "ShouldSerialize");
  186. if (mi != null && !((bool) mi.Invoke (component, null)))
  187. return false;
  188. mi = FindPropertyMethod (component, "Reset");
  189. return mi != null;
  190. }
  191. }
  192. public override bool ShouldSerializeValue (object component)
  193. {
  194. component = MemberDescriptor.GetInvokee (_componentType, component);
  195. if (IsReadOnly) {
  196. MethodInfo mi = FindPropertyMethod (component, "ShouldSerialize");
  197. if (mi != null)
  198. return (bool) mi.Invoke (component, null);
  199. return Attributes.Contains (DesignerSerializationVisibilityAttribute.Content);
  200. }
  201. DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
  202. if (attrib != null) {
  203. object current = GetValue (component);
  204. if (attrib.Value == null || current == null)
  205. return attrib.Value != current;
  206. return !attrib.Value.Equals (current);
  207. }
  208. else {
  209. MethodInfo mi = FindPropertyMethod (component, "ShouldSerialize");
  210. if (mi != null)
  211. return (bool) mi.Invoke (component, null);
  212. // MSDN: If this method cannot find a DefaultValueAttribute or a ShouldSerializeMyProperty method,
  213. // it cannot create optimizations and it returns true.
  214. return true;
  215. }
  216. }
  217. }
  218. }