26
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
using Mono.Collections.Generic;
29
33
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;
35
public sealed class PropertyDefinition : PropertyReference, IMemberDefinition, IConstantProvider {
40
Collection<CustomAttribute> custom_attributes;
42
internal MethodDefinition get_method;
43
internal MethodDefinition set_method;
44
internal Collection<MethodDefinition> other_methods;
46
object constant = Mixin.NotResolved;
47
48
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);
49
get { return (PropertyAttributes) attributes; }
50
set { attributes = (ushort) value; }
55
if (has_this.HasValue)
56
return has_this.Value;
58
if (GetMethod != null)
59
return get_method.HasThis;
61
if (SetMethod != null)
62
return set_method.HasThis;
66
set { has_this = value; }
69
public bool HasCustomAttributes {
71
if (custom_attributes != null)
72
return custom_attributes.Count > 0;
74
return this.GetHasCustomAttributes (Module);
78
public Collection<CustomAttribute> CustomAttributes {
79
get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (Module)); }
80
82
public MethodDefinition GetMethod {
81
get { return m_getMeth; }
82
set { m_getMeth = value; }
84
if (get_method != null)
90
set { get_method = value; }
85
93
public MethodDefinition SetMethod {
86
get { return m_setMeth; }
87
set { m_setMeth = value; }
90
ParameterDefinitionCollection CloneParameterCollection (ParameterDefinitionCollection original)
95
if (set_method != null)
101
set { set_method = value; }
104
public bool HasOtherMethods {
106
if (other_methods != null)
107
return other_methods.Count > 0;
109
InitializeMethods ();
110
return !other_methods.IsNullOrEmpty ();
114
public Collection<MethodDefinition> OtherMethods {
116
if (other_methods != null)
117
return other_methods;
119
InitializeMethods ();
121
if (other_methods != null)
122
return other_methods;
124
return other_methods = new Collection<MethodDefinition> ();
128
public bool HasParameters {
130
if (get_method != null)
131
return get_method.HasParameters;
133
if (set_method != null)
134
return set_method.HasParameters && set_method.Parameters.Count > 1;
140
public override Collection<ParameterDefinition> Parameters {
142
InitializeMethods ();
144
if (get_method != null)
145
return MirrorParameters (get_method, 0);
147
if (set_method != null)
148
return MirrorParameters (set_method, 1);
150
return new Collection<ParameterDefinition> ();
154
static Collection<ParameterDefinition> MirrorParameters (MethodDefinition method, int bound)
92
ParameterDefinitionCollection clone = new ParameterDefinitionCollection (
94
foreach (ParameterDefinition param in original)
156
var parameters = new Collection<ParameterDefinition> ();
157
if (!method.HasParameters)
160
var original_parameters = method.Parameters;
161
var end = original_parameters.Count - bound;
163
for (int i = 0; i < end; i++)
164
parameters.Add (original_parameters [i]);
99
169
public bool HasConstant {
100
get { return m_hasConstant; }
173
return constant != Mixin.NoValue;
175
set { if (!value) constant = Mixin.NoValue; }
103
178
public object Constant {
104
get { return m_const; }
106
m_hasConstant = true;
179
get { return HasConstant ? constant : null; }
180
set { constant = value; }
183
void ResolveConstant ()
185
if (constant != Mixin.NotResolved)
188
this.ResolveConstant (ref constant, Module);
111
191
#region PropertyAttributes
113
193
public bool IsSpecialName {
114
get { return (m_attributes & PropertyAttributes.SpecialName) != 0; }
117
m_attributes |= PropertyAttributes.SpecialName;
119
m_attributes &= ~PropertyAttributes.SpecialName;
194
get { return attributes.GetAttributes ((ushort) PropertyAttributes.SpecialName); }
195
set { attributes = attributes.SetAttributes ((ushort) PropertyAttributes.SpecialName, value); }
123
198
public bool IsRuntimeSpecialName {
124
get { return (m_attributes & PropertyAttributes.RTSpecialName) != 0; }
127
m_attributes |= PropertyAttributes.RTSpecialName;
129
m_attributes &= ~PropertyAttributes.RTSpecialName;
199
get { return attributes.GetAttributes ((ushort) PropertyAttributes.RTSpecialName); }
200
set { attributes = attributes.SetAttributes ((ushort) PropertyAttributes.RTSpecialName, value); }
133
203
public bool HasDefault {
134
get { return (m_attributes & PropertyAttributes.HasDefault) != 0; }
137
m_attributes |= PropertyAttributes.HasDefault;
139
m_attributes &= ~PropertyAttributes.HasDefault;
204
get { return attributes.GetAttributes ((ushort) PropertyAttributes.HasDefault); }
205
set { attributes = attributes.SetAttributes ((ushort) PropertyAttributes.HasDefault, value); }
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.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);
210
public new TypeDefinition DeclaringType {
211
get { return (TypeDefinition) base.DeclaringType; }
212
set { base.DeclaringType = value; }
215
public override bool IsDefinition {
219
public override string FullName {
221
var builder = new StringBuilder ();
222
builder.Append (PropertyType.ToString ());
223
builder.Append (' ');
224
builder.Append (MemberFullName ());
225
builder.Append ('(');
227
var parameters = Parameters;
228
for (int i = 0; i < parameters.Count; i++) {
230
builder.Append (',');
231
builder.Append (parameters [i].ParameterType.FullName);
234
builder.Append (')');
235
return builder.ToString ();
239
public PropertyDefinition (string name, PropertyAttributes attributes, TypeReference propertyType)
240
: base (name, propertyType)
242
this.attributes = (ushort) attributes;
243
this.token = new MetadataToken (TokenType.Property);
246
void InitializeMethods ()
248
if (get_method != null || set_method != null)
251
var module = this.Module;
252
if (!module.HasImage ())
255
module.Read (this, (property, reader) => reader.ReadMethods (property));