ReflectionPropertyDescriptor.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // System.ComponentModel.PropertyDescriptor.cs
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) Novell, Inc.
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.Reflection;
  12. using System.Runtime.InteropServices;
  13. using System.ComponentModel.Design;
  14. namespace System.ComponentModel
  15. {
  16. internal class ReflectionPropertyDescriptor : PropertyDescriptor
  17. {
  18. PropertyInfo _member;
  19. Type _type;
  20. Type _componentType;
  21. public ReflectionPropertyDescriptor (Type componentType, PropertyDescriptor oldPropertyDescriptor, Attribute [] attributes)
  22. : base (oldPropertyDescriptor, attributes)
  23. {
  24. _type = oldPropertyDescriptor.PropertyType;
  25. _componentType = componentType;
  26. }
  27. public ReflectionPropertyDescriptor (Type componentType, string name, Type type, Attribute [] attributes)
  28. : base (name, attributes)
  29. {
  30. _type = type;
  31. _componentType = componentType;
  32. }
  33. public ReflectionPropertyDescriptor (PropertyInfo info)
  34. : base (info.Name, (Attribute[])info.GetCustomAttributes (true))
  35. {
  36. _member = info;
  37. _componentType = _member.DeclaringType;
  38. }
  39. PropertyInfo GetEventInfo ()
  40. {
  41. if (_member == null) {
  42. _member = _componentType.GetProperty (Name);
  43. if (_member == null)
  44. throw new ArgumentException ("Accessor methods for the " + Name + " property are missing");
  45. }
  46. return _member;
  47. }
  48. public override Type ComponentType
  49. {
  50. get { return _componentType; }
  51. }
  52. public override bool IsReadOnly
  53. {
  54. get
  55. {
  56. return !_member.CanWrite;
  57. }
  58. }
  59. public override Type PropertyType
  60. {
  61. get
  62. {
  63. return _member.PropertyType;
  64. }
  65. }
  66. public override object GetValue (object component)
  67. {
  68. return _member.GetValue (component, null);
  69. }
  70. DesignerTransaction CreateTransaction (object obj)
  71. {
  72. Component com = obj as Component;
  73. if (com == null) return null;
  74. IDesignerHost dh = (IDesignerHost) com.Site.GetService (typeof(IDesignerHost));
  75. DesignerTransaction tran = dh.CreateTransaction ();
  76. IComponentChangeService ccs = (IComponentChangeService) com.Site.GetService (typeof(IComponentChangeService));
  77. ccs.OnComponentChanging (com, this);
  78. return tran;
  79. }
  80. void EndTransaction (object obj, DesignerTransaction tran, object oldValue, object newValue, bool commit)
  81. {
  82. if (tran == null) return;
  83. if (commit) {
  84. Component com = obj as Component;
  85. IComponentChangeService ccs = (IComponentChangeService) com.Site.GetService (typeof(IComponentChangeService));
  86. ccs.OnComponentChanged (com, this, oldValue, newValue);
  87. tran.Commit ();
  88. }
  89. else
  90. tran.Cancel ();
  91. }
  92. public override void SetValue (object component, object value)
  93. {
  94. DesignerTransaction tran = CreateTransaction (component);
  95. object old = GetValue (component);
  96. try
  97. {
  98. _member.SetValue (component, value, null);
  99. EndTransaction (component, tran, old, value, true);
  100. }
  101. catch
  102. {
  103. EndTransaction (component, tran, old, value, false);
  104. throw;
  105. }
  106. }
  107. public override void ResetValue (object component)
  108. {
  109. DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
  110. if (attrib != null) {
  111. SetValue (component, attrib.Value);
  112. }
  113. DesignerTransaction tran = CreateTransaction (component);
  114. object old = GetValue (component);
  115. try
  116. {
  117. MethodInfo mi = component.GetType().GetMethod ("Reset" + Name, Type.EmptyTypes);
  118. if (mi != null) mi.Invoke (component, null);
  119. EndTransaction (component, tran, old, GetValue (component), true);
  120. }
  121. catch
  122. {
  123. EndTransaction (component, tran, old, GetValue (component), false);
  124. throw;
  125. }
  126. }
  127. public override bool CanResetValue (object component)
  128. {
  129. DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
  130. if (attrib != null) {
  131. object current = GetValue (component);
  132. if ((attrib.Value == null || current == null) && attrib.Value != current) return true;
  133. return !attrib.Value.Equals (current);
  134. }
  135. else {
  136. MethodInfo mi = component.GetType().GetMethod ("ShouldPersist" + Name, Type.EmptyTypes);
  137. if (mi != null) return (bool) mi.Invoke (component, null);
  138. mi = component.GetType().GetMethod ("Reset" + Name, Type.EmptyTypes);
  139. return (mi != null);
  140. }
  141. }
  142. public override bool ShouldSerializeValue (object component)
  143. {
  144. DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
  145. if (attrib != null) {
  146. object current = GetValue (component);
  147. if ((attrib.Value == null || current == null) && attrib.Value != current) return true;
  148. return !attrib.Value.Equals (current);
  149. }
  150. else {
  151. MethodInfo mi = component.GetType().GetMethod ("ShouldSerialize" + Name, Type.EmptyTypes);
  152. if (mi != null) return (bool) mi.Invoke (component, null);
  153. return true;
  154. }
  155. }
  156. }
  157. }