2
// PropertyDefinition.cs
5
// Jb Evain (jbevain@gmail.com)
7
// Copyright (c) 2008 - 2010 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
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)
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]);
169
public bool HasConstant {
173
return constant != Mixin.NoValue;
175
set { if (!value) constant = Mixin.NoValue; }
178
public object Constant {
179
get { return HasConstant ? constant : null; }
180
set { constant = value; }
183
void ResolveConstant ()
185
if (constant != Mixin.NotResolved)
188
this.ResolveConstant (ref constant, Module);
191
#region PropertyAttributes
193
public bool IsSpecialName {
194
get { return attributes.GetAttributes ((ushort) PropertyAttributes.SpecialName); }
195
set { attributes = attributes.SetAttributes ((ushort) PropertyAttributes.SpecialName, value); }
198
public bool IsRuntimeSpecialName {
199
get { return attributes.GetAttributes ((ushort) PropertyAttributes.RTSpecialName); }
200
set { attributes = attributes.SetAttributes ((ushort) PropertyAttributes.RTSpecialName, value); }
203
public bool HasDefault {
204
get { return attributes.GetAttributes ((ushort) PropertyAttributes.HasDefault); }
205
set { attributes = attributes.SetAttributes ((ushort) PropertyAttributes.HasDefault, value); }
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));