PageRenderTime 48ms CodeModel.GetById 15ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

/Mono.Cecil/PropertyDefinition.cs

http://github.com/jbevain/cecil
C# | 245 lines | 182 code | 54 blank | 9 comment | 40 complexity | 755350217ce7b5c4c083e83e485fd593 MD5 | raw file
  1//
  2// Author:
  3//   Jb Evain (jbevain@gmail.com)
  4//
  5// Copyright (c) 2008 - 2015 Jb Evain
  6// Copyright (c) 2008 - 2011 Novell, Inc.
  7//
  8// Licensed under the MIT/X11 license.
  9//
 10
 11using System.Text;
 12using System.Threading;
 13using Mono.Collections.Generic;
 14
 15namespace Mono.Cecil {
 16
 17	public sealed class PropertyDefinition : PropertyReference, IMemberDefinition, IConstantProvider {
 18
 19		bool? has_this;
 20		ushort attributes;
 21
 22		Collection<CustomAttribute> custom_attributes;
 23
 24		internal MethodDefinition get_method;
 25		internal MethodDefinition set_method;
 26		internal Collection<MethodDefinition> other_methods;
 27
 28		object constant = Mixin.NotResolved;
 29
 30		public PropertyAttributes Attributes {
 31			get { return (PropertyAttributes) attributes; }
 32			set { attributes = (ushort) value; }
 33		}
 34
 35		public bool HasThis {
 36			get {
 37				if (has_this.HasValue)
 38					return has_this.Value;
 39
 40				if (GetMethod != null)
 41					return get_method.HasThis;
 42
 43				if (SetMethod != null)
 44					return set_method.HasThis;
 45
 46				return false;
 47			}
 48			set { has_this = value; }
 49		}
 50
 51		public bool HasCustomAttributes {
 52			get {
 53				if (custom_attributes != null)
 54					return custom_attributes.Count > 0;
 55
 56				return this.GetHasCustomAttributes (Module);
 57			}
 58		}
 59
 60		public Collection<CustomAttribute> CustomAttributes {
 61			get { return custom_attributes ?? (this.GetCustomAttributes (ref custom_attributes, Module)); }
 62		}
 63
 64		public MethodDefinition GetMethod {
 65			get {
 66				if (get_method != null)
 67					return get_method;
 68
 69				InitializeMethods ();
 70				return get_method;
 71			}
 72			set { get_method = value; }
 73		}
 74
 75		public MethodDefinition SetMethod {
 76			get {
 77				if (set_method != null)
 78					return set_method;
 79
 80				InitializeMethods ();
 81				return set_method;
 82			}
 83			set { set_method = value; }
 84		}
 85
 86		public bool HasOtherMethods {
 87			get {
 88				if (other_methods != null)
 89					return other_methods.Count > 0;
 90
 91				InitializeMethods ();
 92				return !other_methods.IsNullOrEmpty ();
 93			}
 94		}
 95
 96		public Collection<MethodDefinition> OtherMethods {
 97			get {
 98				if (other_methods != null)
 99					return other_methods;
100
101				InitializeMethods ();
102
103				if (other_methods != null)
104					return other_methods;
105
106				Interlocked.CompareExchange (ref other_methods, new Collection<MethodDefinition> (), null);
107				return other_methods;
108			}
109		}
110
111		public bool HasParameters {
112			get {
113				InitializeMethods ();
114
115				if (get_method != null)
116					return get_method.HasParameters;
117
118				if (set_method != null)
119					return set_method.HasParameters && set_method.Parameters.Count > 1;
120
121				return false;
122			}
123		}
124
125		public override Collection<ParameterDefinition> Parameters {
126			get {
127				InitializeMethods ();
128
129				if (get_method != null)
130					return MirrorParameters (get_method, 0);
131
132				if (set_method != null)
133					return MirrorParameters (set_method, 1);
134
135				return new Collection<ParameterDefinition> ();
136			}
137		}
138
139		static Collection<ParameterDefinition> MirrorParameters (MethodDefinition method, int bound)
140		{
141			var parameters = new Collection<ParameterDefinition> ();
142			if (!method.HasParameters)
143				return parameters;
144
145			var original_parameters = method.Parameters;
146			var end = original_parameters.Count - bound;
147
148			for (int i = 0; i < end; i++)
149				parameters.Add (original_parameters [i]);
150
151			return parameters;
152		}
153
154		public bool HasConstant {
155			get {
156				this.ResolveConstant (ref constant, Module);
157
158				return constant != Mixin.NoValue;
159			}
160			set { if (!value) constant = Mixin.NoValue; }
161		}
162
163		public object Constant {
164			get { return HasConstant ? constant : null;	}
165			set { constant = value; }
166		}
167
168		#region PropertyAttributes
169
170		public bool IsSpecialName {
171			get { return attributes.GetAttributes ((ushort) PropertyAttributes.SpecialName); }
172			set { attributes = attributes.SetAttributes ((ushort) PropertyAttributes.SpecialName, value); }
173		}
174
175		public bool IsRuntimeSpecialName {
176			get { return attributes.GetAttributes ((ushort) PropertyAttributes.RTSpecialName); }
177			set { attributes = attributes.SetAttributes ((ushort) PropertyAttributes.RTSpecialName, value); }
178		}
179
180		public bool HasDefault {
181			get { return attributes.GetAttributes ((ushort) PropertyAttributes.HasDefault); }
182			set { attributes = attributes.SetAttributes ((ushort) PropertyAttributes.HasDefault, value); }
183		}
184
185		#endregion
186
187		public new TypeDefinition DeclaringType {
188			get { return (TypeDefinition) base.DeclaringType; }
189			set { base.DeclaringType = value; }
190		}
191
192		public override bool IsDefinition {
193			get { return true; }
194		}
195
196		public override string FullName {
197			get {
198				var builder = new StringBuilder ();
199				builder.Append (PropertyType.ToString ());
200				builder.Append (' ');
201				builder.Append (MemberFullName ());
202				builder.Append ('(');
203				if (HasParameters) {
204					var parameters = Parameters;
205					for (int i = 0; i < parameters.Count; i++) {
206						if (i > 0)
207							builder.Append (',');
208						builder.Append (parameters [i].ParameterType.FullName);
209					}
210				}
211				builder.Append (')');
212				return builder.ToString ();
213			}
214		}
215
216		public PropertyDefinition (string name, PropertyAttributes attributes, TypeReference propertyType)
217			: base (name, propertyType)
218		{
219			this.attributes = (ushort) attributes;
220			this.token = new MetadataToken (TokenType.Property);
221		}
222
223		void InitializeMethods ()
224		{
225			var module = this.Module;
226			if (module == null)
227				return;
228
229			lock (module.SyncRoot) {
230				if (get_method != null || set_method != null)
231					return;
232
233				if (!module.HasImage ())
234					return;
235
236				module.Read (this, (property, reader) => reader.ReadMethods (property));
237			}
238		}
239
240		public override PropertyDefinition Resolve ()
241		{
242			return this;
243		}
244	}
245}