PropertyDefinition.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // PropertyDefinition.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@gmail.com)
  6. //
  7. // Copyright (c) 2008 - 2011 Jb Evain
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Text;
  29. using Mono.Collections.Generic;
  30. namespace Mono.Cecil {
  31. public sealed class PropertyDefinition : PropertyReference, IMemberDefinition, IConstantProvider {
  32. bool? has_this;
  33. ushort attributes;
  34. Collection<CustomAttribute> custom_attributes;
  35. internal MethodDefinition get_method;
  36. internal MethodDefinition set_method;
  37. internal Collection<MethodDefinition> other_methods;
  38. object constant = Mixin.NotResolved;
  39. public PropertyAttributes Attributes {
  40. get { return (PropertyAttributes) attributes; }
  41. set { attributes = (ushort) value; }
  42. }
  43. public bool HasThis {
  44. get {
  45. if (has_this.HasValue)
  46. return has_this.Value;
  47. if (GetMethod != null)
  48. return get_method.HasThis;
  49. if (SetMethod != null)
  50. return set_method.HasThis;
  51. return false;
  52. }
  53. set { has_this = value; }
  54. }
  55. public bool HasCustomAttributes {
  56. get {
  57. if (custom_attributes != null)
  58. return custom_attributes.Count > 0;
  59. return Mixin.GetHasCustomAttributes(this, Module);
  60. }
  61. }
  62. public Collection<CustomAttribute> CustomAttributes {
  63. get { return custom_attributes ?? (Mixin.GetCustomAttributes(this, ref custom_attributes, Module)); }
  64. }
  65. public MethodDefinition GetMethod {
  66. get {
  67. if (get_method != null)
  68. return get_method;
  69. InitializeMethods ();
  70. return get_method;
  71. }
  72. set { get_method = value; }
  73. }
  74. public MethodDefinition SetMethod {
  75. get {
  76. if (set_method != null)
  77. return set_method;
  78. InitializeMethods ();
  79. return set_method;
  80. }
  81. set { set_method = value; }
  82. }
  83. public bool HasOtherMethods {
  84. get {
  85. if (other_methods != null)
  86. return other_methods.Count > 0;
  87. InitializeMethods ();
  88. return !Mixin.IsNullOrEmpty (other_methods);
  89. }
  90. }
  91. public Collection<MethodDefinition> OtherMethods {
  92. get {
  93. if (other_methods != null)
  94. return other_methods;
  95. InitializeMethods ();
  96. if (other_methods != null)
  97. return other_methods;
  98. return other_methods = new Collection<MethodDefinition> ();
  99. }
  100. }
  101. public bool HasParameters {
  102. get {
  103. InitializeMethods ();
  104. if (get_method != null)
  105. return get_method.HasParameters;
  106. if (set_method != null)
  107. return set_method.HasParameters && set_method.Parameters.Count > 1;
  108. return false;
  109. }
  110. }
  111. public override Collection<ParameterDefinition> Parameters {
  112. get {
  113. InitializeMethods ();
  114. if (get_method != null)
  115. return MirrorParameters (get_method, 0);
  116. if (set_method != null)
  117. return MirrorParameters (set_method, 1);
  118. return new Collection<ParameterDefinition> ();
  119. }
  120. }
  121. static Collection<ParameterDefinition> MirrorParameters (MethodDefinition method, int bound)
  122. {
  123. var parameters = new Collection<ParameterDefinition> ();
  124. if (!method.HasParameters)
  125. return parameters;
  126. var original_parameters = method.Parameters;
  127. var end = original_parameters.Count - bound;
  128. for (int i = 0; i < end; i++)
  129. parameters.Add (original_parameters [i]);
  130. return parameters;
  131. }
  132. public bool HasConstant {
  133. get {
  134. Mixin.ResolveConstant(this, ref constant, Module);
  135. return constant != Mixin.NoValue;
  136. }
  137. set { if (!value) constant = Mixin.NoValue; }
  138. }
  139. public object Constant {
  140. get { return HasConstant ? constant : null; }
  141. set { constant = value; }
  142. }
  143. #region PropertyAttributes
  144. public bool IsSpecialName {
  145. get { return Mixin.GetAttributes(attributes,(ushort) PropertyAttributes.SpecialName); }
  146. set { attributes =Mixin.SetAttributes(attributes,(ushort) PropertyAttributes.SpecialName, value); }
  147. }
  148. public bool IsRuntimeSpecialName {
  149. get { return Mixin.GetAttributes(attributes,(ushort) PropertyAttributes.RTSpecialName); }
  150. set { attributes =Mixin.SetAttributes(attributes,(ushort) PropertyAttributes.RTSpecialName, value); }
  151. }
  152. public bool HasDefault {
  153. get { return Mixin.GetAttributes(attributes,(ushort) PropertyAttributes.HasDefault); }
  154. set { attributes =Mixin.SetAttributes(attributes,(ushort) PropertyAttributes.HasDefault, value); }
  155. }
  156. #endregion
  157. public new TypeDefinition DeclaringType {
  158. get { return (TypeDefinition) base.DeclaringType; }
  159. set { base.DeclaringType = value; }
  160. }
  161. public override bool IsDefinition {
  162. get { return true; }
  163. }
  164. public override string FullName {
  165. get {
  166. var builder = new StringBuilder ();
  167. builder.Append (PropertyType.ToString ());
  168. builder.Append (' ');
  169. builder.Append (MemberFullName ());
  170. builder.Append ('(');
  171. if (HasParameters) {
  172. var parameters = Parameters;
  173. for (int i = 0; i < parameters.Count; i++) {
  174. if (i > 0)
  175. builder.Append (',');
  176. builder.Append (parameters [i].ParameterType.FullName);
  177. }
  178. }
  179. builder.Append (')');
  180. return builder.ToString ();
  181. }
  182. }
  183. public PropertyDefinition (string name, PropertyAttributes attributes, TypeReference propertyType)
  184. : base (name, propertyType)
  185. {
  186. this.attributes = (ushort) attributes;
  187. this.token = new MetadataToken (TokenType.Property);
  188. }
  189. void InitializeMethods ()
  190. {
  191. var module = this.Module;
  192. lock (module.SyncRoot) {
  193. if (get_method != null || set_method != null)
  194. return;
  195. if (!Mixin.HasImage(module))
  196. return;
  197. module.Read (this, (property, reader) => reader.ReadMethods (property));
  198. }
  199. }
  200. public override PropertyDefinition Resolve ()
  201. {
  202. return this;
  203. }
  204. }
  205. }