2
// PropertyDefinition.cs
5
// Jb Evain (jbevain@gmail.com)
7
// Copyright (c) 2008 - 2011 Jb Evain
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.
31
using Mono.Collections.Generic;
33
namespace Mono.Cecil {
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;
48
public PropertyAttributes Attributes {
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)); }
82
public MethodDefinition GetMethod {
84
if (get_method != null)
90
set { get_method = value; }
93
public MethodDefinition SetMethod {
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
InitializeMethods ();
132
if (get_method != null)
133
return get_method.HasParameters;
135
if (set_method != null)
136
return set_method.HasParameters && set_method.Parameters.Count > 1;
142
public override Collection<ParameterDefinition> Parameters {
144
InitializeMethods ();
146
if (get_method != null)
147
return MirrorParameters (get_method, 0);
149
if (set_method != null)
150
return MirrorParameters (set_method, 1);
152
return new Collection<ParameterDefinition> ();
156
static Collection<ParameterDefinition> MirrorParameters (MethodDefinition method, int bound)
158
var parameters = new Collection<ParameterDefinition> ();
159
if (!method.HasParameters)
162
var original_parameters = method.Parameters;
163
var end = original_parameters.Count - bound;
165
for (int i = 0; i < end; i++)
166
parameters.Add (original_parameters [i]);
171
public bool HasConstant {
175
return constant != Mixin.NoValue;
177
set { if (!value) constant = Mixin.NoValue; }
180
public object Constant {
181
get { return HasConstant ? constant : null; }
182
set { constant = value; }
185
void ResolveConstant ()
187
if (constant != Mixin.NotResolved)
190
this.ResolveConstant (ref constant, Module);
193
#region PropertyAttributes
195
public bool IsSpecialName {
196
get { return attributes.GetAttributes ((ushort) PropertyAttributes.SpecialName); }
197
set { attributes = attributes.SetAttributes ((ushort) PropertyAttributes.SpecialName, value); }
200
public bool IsRuntimeSpecialName {
201
get { return attributes.GetAttributes ((ushort) PropertyAttributes.RTSpecialName); }
202
set { attributes = attributes.SetAttributes ((ushort) PropertyAttributes.RTSpecialName, value); }
205
public bool HasDefault {
206
get { return attributes.GetAttributes ((ushort) PropertyAttributes.HasDefault); }
207
set { attributes = attributes.SetAttributes ((ushort) PropertyAttributes.HasDefault, value); }
212
public new TypeDefinition DeclaringType {
213
get { return (TypeDefinition) base.DeclaringType; }
214
set { base.DeclaringType = value; }
217
public override bool IsDefinition {
221
public override string FullName {
223
var builder = new StringBuilder ();
224
builder.Append (PropertyType.ToString ());
225
builder.Append (' ');
226
builder.Append (MemberFullName ());
227
builder.Append ('(');
229
var parameters = Parameters;
230
for (int i = 0; i < parameters.Count; i++) {
232
builder.Append (',');
233
builder.Append (parameters [i].ParameterType.FullName);
236
builder.Append (')');
237
return builder.ToString ();
241
public PropertyDefinition (string name, PropertyAttributes attributes, TypeReference propertyType)
242
: base (name, propertyType)
244
this.attributes = (ushort) attributes;
245
this.token = new MetadataToken (TokenType.Property);
248
void InitializeMethods ()
250
if (get_method != null || set_method != null)
253
var module = this.Module;
254
if (!module.HasImage ())
257
module.Read (this, (property, reader) => reader.ReadMethods (property));
260
public override PropertyDefinition Resolve ()