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
public 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 CustomAttributeCollection CustomAttributes {
54
if (m_customAttrs == null)
55
m_customAttrs = new CustomAttributeCollection (this);
61
public override ParameterDefinitionCollection Parameters {
63
if (this.GetMethod != null)
64
return CloneParameterCollection (this.GetMethod.Parameters);
65
else if (this.SetMethod != null) {
66
ParameterDefinitionCollection parameters =
67
CloneParameterCollection (this.SetMethod.Parameters);
68
if (parameters.Count > 0)
69
parameters.RemoveAt (parameters.Count - 1);
73
if (m_parameters == null)
74
m_parameters = new ParameterDefinitionCollection (this);
80
public MethodDefinition GetMethod {
81
get { return m_getMeth; }
82
set { m_getMeth = value; }
85
public MethodDefinition SetMethod {
86
get { return m_setMeth; }
87
set { m_setMeth = value; }
90
ParameterDefinitionCollection CloneParameterCollection (ParameterDefinitionCollection original)
92
ParameterDefinitionCollection clone = new ParameterDefinitionCollection (
94
foreach (ParameterDefinition param in original)
99
public bool HasConstant {
100
get { return m_hasConstant; }
103
public object Constant {
104
get { return m_const; }
106
m_hasConstant = true;
111
#region PropertyAttributes
113
public bool IsSpecialName {
114
get { return (m_attributes & PropertyAttributes.SpecialName) != 0; }
117
m_attributes |= PropertyAttributes.SpecialName;
119
m_attributes &= ~PropertyAttributes.SpecialName;
123
public bool IsRuntimeSpecialName {
124
get { return (m_attributes & PropertyAttributes.RTSpecialName) != 0; }
127
m_attributes |= PropertyAttributes.RTSpecialName;
129
m_attributes &= ~PropertyAttributes.RTSpecialName;
133
public bool HasDefault {
134
get { return (m_attributes & PropertyAttributes.HasDefault) != 0; }
137
m_attributes |= PropertyAttributes.HasDefault;
139
m_attributes &= ~PropertyAttributes.HasDefault;
145
public PropertyDefinition (string name, TypeReference propertyType, PropertyAttributes attrs) : base (name, propertyType)
147
m_attributes = attrs;
150
public static MethodDefinition CreateGetMethod (PropertyDefinition prop)
152
MethodDefinition get = new MethodDefinition (
153
string.Concat ("get_", prop.Name), (MethodAttributes) 0, prop.PropertyType);
154
prop.GetMethod = get;
158
public static MethodDefinition CreateSetMethod (PropertyDefinition prop)
160
MethodDefinition set = new MethodDefinition (
161
string.Concat ("set_", prop.Name), (MethodAttributes) 0, prop.PropertyType);
162
prop.SetMethod = set;
166
public PropertyDefinition Clone ()
168
return Clone (this, new ImportContext (NullReferenceImporter.Instance, this.DeclaringType));
171
internal static PropertyDefinition Clone (PropertyDefinition prop, ImportContext context)
173
PropertyDefinition np = new PropertyDefinition (
175
context.Import (prop.PropertyType),
178
if (prop.HasConstant)
179
np.Constant = prop.Constant;
181
if (context != null && context.GenericContext.Type is TypeDefinition) {
182
TypeDefinition type = context.GenericContext.Type as TypeDefinition;
183
if (prop.SetMethod != null)
184
np.SetMethod = type.Methods.GetMethod (prop.SetMethod.Name, prop.SetMethod.Parameters);
185
if (prop.GetMethod != null)
186
np.GetMethod = type.Methods.GetMethod (prop.GetMethod.Name, prop.GetMethod.Parameters);
189
foreach (CustomAttribute ca in prop.CustomAttributes)
190
np.CustomAttributes.Add (CustomAttribute.Clone (ca, context));
195
public override string ToString ()
197
StringBuilder sb = new StringBuilder ();
198
sb.Append (PropertyType.ToString ());
201
if (this.DeclaringType != null) {
202
sb.Append (this.DeclaringType.ToString ());
206
sb.Append (this.Name);
208
ParameterDefinitionCollection parameters = this.Parameters;
209
for (int i = 0; i < parameters.Count; i++) {
212
sb.Append (parameters [i].ParameterType.ToString ());
215
return sb.ToString ();
218
public override void Accept (IReflectionVisitor visitor)
220
visitor.VisitPropertyDefinition (this);
222
this.CustomAttributes.Accept (visitor);