ReflectionPropertyDescriptor.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 _type;
  40. Type _componentType;
  41. public ReflectionPropertyDescriptor (Type componentType, PropertyDescriptor oldPropertyDescriptor, Attribute [] attributes)
  42. : base (oldPropertyDescriptor, attributes)
  43. {
  44. _type = oldPropertyDescriptor.PropertyType;
  45. _componentType = componentType;
  46. }
  47. public ReflectionPropertyDescriptor (Type componentType, string name, Type type, Attribute [] attributes)
  48. : base (name, attributes)
  49. {
  50. _type = type;
  51. _componentType = componentType;
  52. }
  53. public ReflectionPropertyDescriptor (PropertyInfo info)
  54. : base (info.Name, (Attribute[])info.GetCustomAttributes (true))
  55. {
  56. _member = info;
  57. _componentType = _member.DeclaringType;
  58. }
  59. PropertyInfo GetPropertyInfo ()
  60. {
  61. if (_member == null) {
  62. _member = _componentType.GetProperty (Name);
  63. if (_member == null)
  64. throw new ArgumentException ("Accessor methods for the " + Name + " property are missing");
  65. }
  66. return _member;
  67. }
  68. public override Type ComponentType
  69. {
  70. get { return _componentType; }
  71. }
  72. public override bool IsReadOnly
  73. {
  74. get
  75. {
  76. return !GetPropertyInfo ().CanWrite;
  77. }
  78. }
  79. public override Type PropertyType
  80. {
  81. get
  82. {
  83. return GetPropertyInfo ().PropertyType;
  84. }
  85. }
  86. public override object GetValue (object component)
  87. {
  88. return GetPropertyInfo ().GetValue (component, null);
  89. }
  90. DesignerTransaction CreateTransaction (object obj)
  91. {
  92. Component com = obj as Component;
  93. if (com == null || com.Site == null) return null;
  94. IDesignerHost dh = (IDesignerHost) com.Site.GetService (typeof(IDesignerHost));
  95. if (dh == null) 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) return;
  105. if (commit) {
  106. Component com = obj as Component;
  107. IComponentChangeService ccs = (IComponentChangeService) com.Site.GetService (typeof(IComponentChangeService));
  108. if (ccs != null)
  109. ccs.OnComponentChanged (com, this, oldValue, newValue);
  110. tran.Commit ();
  111. }
  112. else
  113. tran.Cancel ();
  114. }
  115. public override void SetValue (object component, object value)
  116. {
  117. DesignerTransaction tran = CreateTransaction (component);
  118. object old = GetValue (component);
  119. try
  120. {
  121. GetPropertyInfo ().SetValue (component, value, null);
  122. EndTransaction (component, tran, old, value, true);
  123. }
  124. catch
  125. {
  126. EndTransaction (component, tran, old, value, false);
  127. throw;
  128. }
  129. }
  130. public override void ResetValue (object component)
  131. {
  132. DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
  133. if (attrib != null) {
  134. SetValue (component, attrib.Value);
  135. }
  136. DesignerTransaction tran = CreateTransaction (component);
  137. object old = GetValue (component);
  138. try
  139. {
  140. MethodInfo mi = component.GetType().GetMethod ("Reset" + Name, Type.EmptyTypes);
  141. if (mi != null) mi.Invoke (component, null);
  142. EndTransaction (component, tran, old, GetValue (component), true);
  143. }
  144. catch
  145. {
  146. EndTransaction (component, tran, old, GetValue (component), false);
  147. throw;
  148. }
  149. }
  150. public override bool CanResetValue (object component)
  151. {
  152. DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
  153. if (attrib != null) {
  154. object current = GetValue (component);
  155. if ((attrib.Value == null || current == null) && attrib.Value != current) return true;
  156. return !attrib.Value.Equals (current);
  157. }
  158. else {
  159. MethodInfo mi = component.GetType().GetMethod ("ShouldPersist" + Name, Type.EmptyTypes);
  160. if (mi != null) return (bool) mi.Invoke (component, null);
  161. mi = component.GetType().GetMethod ("Reset" + Name, Type.EmptyTypes);
  162. return (mi != null);
  163. }
  164. }
  165. public override bool ShouldSerializeValue (object component)
  166. {
  167. DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
  168. if (attrib != null) {
  169. object current = GetValue (component);
  170. if ((attrib.Value == null || current == null) && attrib.Value != current) return true;
  171. return !attrib.Value.Equals (current);
  172. }
  173. else {
  174. MethodInfo mi = component.GetType().GetMethod ("ShouldSerialize" + Name, Type.EmptyTypes);
  175. if (mi != null) return (bool) mi.Invoke (component, null);
  176. return true;
  177. }
  178. }
  179. }
  180. }