| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- //
- // PropertyDefinition.cs
- //
- // Author:
- // Jb Evain (jbevain@gmail.com)
- //
- // Copyright (c) 2008 - 2011 Jb Evain
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System.Text;
- using Mono.Collections.Generic;
- namespace Mono.Cecil {
- public sealed class PropertyDefinition : PropertyReference, IMemberDefinition, IConstantProvider {
- bool? has_this;
- ushort attributes;
- Collection<CustomAttribute> custom_attributes;
- internal MethodDefinition get_method;
- internal MethodDefinition set_method;
- internal Collection<MethodDefinition> other_methods;
- object constant = Mixin.NotResolved;
- public PropertyAttributes Attributes {
- get { return (PropertyAttributes) attributes; }
- set { attributes = (ushort) value; }
- }
- public bool HasThis {
- get {
- if (has_this.HasValue)
- return has_this.Value;
- if (GetMethod != null)
- return get_method.HasThis;
- if (SetMethod != null)
- return set_method.HasThis;
- return false;
- }
- set { has_this = value; }
- }
- public bool HasCustomAttributes {
- get {
- if (custom_attributes != null)
- return custom_attributes.Count > 0;
- return Mixin.GetHasCustomAttributes(this, Module);
- }
- }
- public Collection<CustomAttribute> CustomAttributes {
- get { return custom_attributes ?? (Mixin.GetCustomAttributes(this, ref custom_attributes, Module)); }
- }
- public MethodDefinition GetMethod {
- get {
- if (get_method != null)
- return get_method;
- InitializeMethods ();
- return get_method;
- }
- set { get_method = value; }
- }
- public MethodDefinition SetMethod {
- get {
- if (set_method != null)
- return set_method;
- InitializeMethods ();
- return set_method;
- }
- set { set_method = value; }
- }
- public bool HasOtherMethods {
- get {
- if (other_methods != null)
- return other_methods.Count > 0;
- InitializeMethods ();
- return !Mixin.IsNullOrEmpty (other_methods);
- }
- }
- public Collection<MethodDefinition> OtherMethods {
- get {
- if (other_methods != null)
- return other_methods;
- InitializeMethods ();
- if (other_methods != null)
- return other_methods;
- return other_methods = new Collection<MethodDefinition> ();
- }
- }
- public bool HasParameters {
- get {
- InitializeMethods ();
- if (get_method != null)
- return get_method.HasParameters;
- if (set_method != null)
- return set_method.HasParameters && set_method.Parameters.Count > 1;
- return false;
- }
- }
- public override Collection<ParameterDefinition> Parameters {
- get {
- InitializeMethods ();
- if (get_method != null)
- return MirrorParameters (get_method, 0);
- if (set_method != null)
- return MirrorParameters (set_method, 1);
- return new Collection<ParameterDefinition> ();
- }
- }
- static Collection<ParameterDefinition> MirrorParameters (MethodDefinition method, int bound)
- {
- var parameters = new Collection<ParameterDefinition> ();
- if (!method.HasParameters)
- return parameters;
- var original_parameters = method.Parameters;
- var end = original_parameters.Count - bound;
- for (int i = 0; i < end; i++)
- parameters.Add (original_parameters [i]);
- return parameters;
- }
- public bool HasConstant {
- get {
- Mixin.ResolveConstant(this, ref constant, Module);
- return constant != Mixin.NoValue;
- }
- set { if (!value) constant = Mixin.NoValue; }
- }
- public object Constant {
- get { return HasConstant ? constant : null; }
- set { constant = value; }
- }
- #region PropertyAttributes
- public bool IsSpecialName {
- get { return Mixin.GetAttributes(attributes,(ushort) PropertyAttributes.SpecialName); }
- set { attributes =Mixin.SetAttributes(attributes,(ushort) PropertyAttributes.SpecialName, value); }
- }
- public bool IsRuntimeSpecialName {
- get { return Mixin.GetAttributes(attributes,(ushort) PropertyAttributes.RTSpecialName); }
- set { attributes =Mixin.SetAttributes(attributes,(ushort) PropertyAttributes.RTSpecialName, value); }
- }
- public bool HasDefault {
- get { return Mixin.GetAttributes(attributes,(ushort) PropertyAttributes.HasDefault); }
- set { attributes =Mixin.SetAttributes(attributes,(ushort) PropertyAttributes.HasDefault, value); }
- }
- #endregion
- public new TypeDefinition DeclaringType {
- get { return (TypeDefinition) base.DeclaringType; }
- set { base.DeclaringType = value; }
- }
- public override bool IsDefinition {
- get { return true; }
- }
- public override string FullName {
- get {
- var builder = new StringBuilder ();
- builder.Append (PropertyType.ToString ());
- builder.Append (' ');
- builder.Append (MemberFullName ());
- builder.Append ('(');
- if (HasParameters) {
- var parameters = Parameters;
- for (int i = 0; i < parameters.Count; i++) {
- if (i > 0)
- builder.Append (',');
- builder.Append (parameters [i].ParameterType.FullName);
- }
- }
- builder.Append (')');
- return builder.ToString ();
- }
- }
- public PropertyDefinition (string name, PropertyAttributes attributes, TypeReference propertyType)
- : base (name, propertyType)
- {
- this.attributes = (ushort) attributes;
- this.token = new MetadataToken (TokenType.Property);
- }
- void InitializeMethods ()
- {
- var module = this.Module;
- lock (module.SyncRoot) {
- if (get_method != null || set_method != null)
- return;
- if (!Mixin.HasImage(module))
- return;
- module.Read (this, (property, reader) => reader.ReadMethods (property));
- }
- }
- public override PropertyDefinition Resolve ()
- {
- return this;
- }
- }
- }
|