2 // PropertyDefinition.cs
5 // Jb Evain (jbevain@gmail.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 namespace Mono.Cecil {
34 internal sealed class PropertyDefinition : PropertyReference,
35 IMemberDefinition, ICustomAttributeProvider, IHasConstant {
37 PropertyAttributes m_attributes;
39 CustomAttributeCollection m_customAttrs;
41 MethodDefinition m_getMeth;
42 MethodDefinition m_setMeth;
47 public PropertyAttributes Attributes {
48 get { return m_attributes; }
49 set { m_attributes = value; }
52 public bool HasCustomAttributes {
53 get { return (m_customAttrs == null) ? false : (m_customAttrs.Count > 0); }
56 public CustomAttributeCollection CustomAttributes {
58 if (m_customAttrs == null)
59 m_customAttrs = new CustomAttributeCollection (this);
65 public override bool HasParameters {
67 if (m_getMeth != null)
68 return m_getMeth.HasParameters;
69 else if (m_setMeth != null)
70 return m_setMeth.HasParameters;
71 else if (m_parameters == null)
74 return m_parameters.Count > 0;
78 public override ParameterDefinitionCollection Parameters {
80 if (this.GetMethod != null)
81 return CloneParameterCollection (this.GetMethod.Parameters);
82 else if (this.SetMethod != null) {
83 ParameterDefinitionCollection parameters =
84 CloneParameterCollection (this.SetMethod.Parameters);
85 if (parameters.Count > 0)
86 parameters.RemoveAt (parameters.Count - 1);
90 if (m_parameters == null)
91 m_parameters = new ParameterDefinitionCollection (this);
97 public MethodDefinition GetMethod {
98 get { return m_getMeth; }
99 set { m_getMeth = value; }
102 public MethodDefinition SetMethod {
103 get { return m_setMeth; }
104 set { m_setMeth = value; }
107 ParameterDefinitionCollection CloneParameterCollection (ParameterDefinitionCollection original)
109 ParameterDefinitionCollection clone = new ParameterDefinitionCollection (
111 foreach (ParameterDefinition param in original)
116 public bool HasConstant {
117 get { return m_hasConstant; }
120 public object Constant {
121 get { return m_const; }
123 m_hasConstant = true;
128 #region PropertyAttributes
130 public bool IsSpecialName {
131 get { return (m_attributes & PropertyAttributes.SpecialName) != 0; }
134 m_attributes |= PropertyAttributes.SpecialName;
136 m_attributes &= ~PropertyAttributes.SpecialName;
140 public bool IsRuntimeSpecialName {
141 get { return (m_attributes & PropertyAttributes.RTSpecialName) != 0; }
144 m_attributes |= PropertyAttributes.RTSpecialName;
146 m_attributes &= ~PropertyAttributes.RTSpecialName;
150 public bool HasDefault {
151 get { return (m_attributes & PropertyAttributes.HasDefault) != 0; }
154 m_attributes |= PropertyAttributes.HasDefault;
156 m_attributes &= ~PropertyAttributes.HasDefault;
162 public new TypeDefinition DeclaringType {
163 get { return (TypeDefinition) base.DeclaringType; }
164 set { base.DeclaringType = value; }
167 public PropertyDefinition (string name, TypeReference propertyType, PropertyAttributes attrs) : base (name, propertyType)
169 m_attributes = attrs;
172 public override PropertyDefinition Resolve ()
177 public static MethodDefinition CreateGetMethod (PropertyDefinition prop)
179 MethodDefinition get = new MethodDefinition (
180 string.Concat ("get_", prop.Name), (MethodAttributes) 0, prop.PropertyType);
181 prop.GetMethod = get;
185 public static MethodDefinition CreateSetMethod (PropertyDefinition prop)
187 MethodDefinition set = new MethodDefinition (
188 string.Concat ("set_", prop.Name), (MethodAttributes) 0, prop.PropertyType);
189 prop.SetMethod = set;
193 public PropertyDefinition Clone ()
195 return Clone (this, new ImportContext (NullReferenceImporter.Instance, this.DeclaringType));
198 internal static PropertyDefinition Clone (PropertyDefinition prop, ImportContext context)
200 PropertyDefinition np = new PropertyDefinition (
202 context.Import (prop.PropertyType),
205 if (prop.HasConstant)
206 np.Constant = prop.Constant;
208 if (context.GenericContext.Type is TypeDefinition) {
209 TypeDefinition type = context.GenericContext.Type as TypeDefinition;
210 if (prop.SetMethod != null)
211 np.SetMethod = type.Methods.GetMethod (prop.SetMethod.Name, prop.SetMethod.Parameters);
212 if (prop.GetMethod != null)
213 np.GetMethod = type.Methods.GetMethod (prop.GetMethod.Name, prop.GetMethod.Parameters);
216 foreach (CustomAttribute ca in prop.CustomAttributes)
217 np.CustomAttributes.Add (CustomAttribute.Clone (ca, context));
222 public override string ToString ()
224 StringBuilder sb = new StringBuilder ();
225 sb.Append (PropertyType.ToString ());
228 if (this.DeclaringType != null) {
229 sb.Append (this.DeclaringType.ToString ());
233 sb.Append (this.Name);
235 ParameterDefinitionCollection parameters = this.Parameters;
236 for (int i = 0; i < parameters.Count; i++) {
239 sb.Append (parameters [i].ParameterType.ToString ());
242 return sb.ToString ();
245 public override void Accept (IReflectionVisitor visitor)
247 visitor.VisitPropertyDefinition (this);
249 this.CustomAttributes.Accept (visitor);