ReflectionPropertyDescriptor.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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)
  91. return null;
  92. IDesignerHost dh = (IDesignerHost) com.Site.GetService (typeof(IDesignerHost));
  93. if (dh == null)
  94. return null;
  95. DesignerTransaction tran = dh.CreateTransaction ();
  96. IComponentChangeService ccs = (IComponentChangeService) com.Site.GetService (typeof(IComponentChangeService));
  97. if (ccs != null)
  98. ccs.OnComponentChanging (com, this);
  99. return tran;
  100. }
  101. void EndTransaction (object obj, DesignerTransaction tran, object oldValue, object newValue, bool commit)
  102. {
  103. if (tran == null)
  104. return;
  105. if (commit) {
  106. IComponent com = obj as IComponent;
  107. IComponentChangeService ccs = (IComponentChangeService) com.Site.GetService (typeof(IComponentChangeService));
  108. if (ccs != null)
  109. ccs.OnComponentChanged (com, this, oldValue, newValue);
  110. tran.Commit ();
  111. } else
  112. tran.Cancel ();
  113. }
  114. public override void SetValue (object component, object value)
  115. {
  116. DesignerTransaction tran = CreateTransaction (component);
  117. object old = GetValue (component);
  118. try {
  119. GetPropertyInfo ().SetValue (component, value, null);
  120. EndTransaction (component, tran, old, value, true);
  121. } catch {
  122. EndTransaction (component, tran, old, value, false);
  123. throw;
  124. }
  125. }
  126. public override void ResetValue (object component)
  127. {
  128. DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
  129. if (attrib != null)
  130. SetValue (component, attrib.Value);
  131. DesignerTransaction tran = CreateTransaction (component);
  132. object old = GetValue (component);
  133. try {
  134. MethodInfo mi = component.GetType().GetMethod ("Reset" + Name, Type.EmptyTypes);
  135. if (mi != null)
  136. mi.Invoke (component, null);
  137. EndTransaction (component, tran, old, GetValue (component), true);
  138. } catch {
  139. EndTransaction (component, tran, old, GetValue (component), false);
  140. throw;
  141. }
  142. }
  143. public override bool CanResetValue (object component)
  144. {
  145. DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
  146. if (attrib != null) {
  147. object current = GetValue (component);
  148. if (attrib.Value == null || current == null){
  149. if (attrib.Value != current)
  150. return true;
  151. if (attrib.Value == null && current == null)
  152. return false;
  153. }
  154. return !attrib.Value.Equals (current);
  155. } else {
  156. MethodInfo mi = component.GetType().GetMethod ("ShouldPersist" + Name, Type.EmptyTypes);
  157. if (mi != null)
  158. return (bool) mi.Invoke (component, null);
  159. mi = component.GetType().GetMethod ("Reset" + Name, Type.EmptyTypes);
  160. return mi != null;
  161. }
  162. }
  163. public override bool ShouldSerializeValue (object component)
  164. {
  165. DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
  166. if (attrib != null) {
  167. object current = GetValue (component);
  168. if ((attrib.Value == null || current == null) && attrib.Value != current)
  169. return true;
  170. return !attrib.Value.Equals (current);
  171. }
  172. else {
  173. MethodInfo mi = component.GetType().GetMethod ("ShouldSerialize" + Name, Type.EmptyTypes);
  174. if (mi != null)
  175. return (bool) mi.Invoke (component, null);
  176. return true;
  177. }
  178. }
  179. }
  180. }