2 // Copyright (c) 2004, Rodrigo B. de Oliveira (rbo@acm.org)
3 // All rights reserved.
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of Rodrigo B. de Oliveira nor the names of its
14 // contributors may be used to endorse or promote products derived from this
15 // software without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 using System
.Reflection
;
32 namespace Boo
.Lang
.Compiler
.TypeSystem
34 public class ExternalProperty
: ExternalEntity
<System
.Reflection
.PropertyInfo
>, IProperty
36 private IParameter
[] _parameters
;
38 private bool? _isBooExtension
;
39 private bool? _isClrExtension
;
41 private System
.Reflection
.MethodInfo _accessor
= null;
43 private CachedMethod _getter
= null;
45 private CachedMethod _setter
= null;
47 public ExternalProperty(TypeSystemServices typeSystemServices
, System
.Reflection
.PropertyInfo property
) : base(typeSystemServices
, property
)
51 public bool IsExtension
55 return IsBooExtension
|| IsClrExtension
;
59 public bool IsBooExtension
63 if (null == _isBooExtension
)
65 _isBooExtension
= MetadataUtil
.IsAttributeDefined(_memberInfo
, Types
.BooExtensionAttribute
);
67 return _isBooExtension
.Value
;
71 public bool IsClrExtension
75 if (null == _isClrExtension
)
77 _isClrExtension
= MetadataUtil
.HasClrExtensions()
79 && MetadataUtil
.IsAttributeDefined(_memberInfo
, Types
.ClrExtensionAttribute
);
81 return _isClrExtension
.Value
;
85 public virtual IType DeclaringType
89 return _typeSystemServices
.Map(_memberInfo
.DeclaringType
);
97 return GetAccessor().IsStatic
;
105 return GetAccessor().IsPublic
;
109 public bool IsProtected
113 System
.Reflection
.MethodInfo accessor
= GetAccessor();
114 return accessor
.IsFamily
|| accessor
.IsFamilyOrAssembly
;
118 public bool IsInternal
122 return GetAccessor().IsAssembly
;
126 public bool IsPrivate
130 return GetAccessor().IsPrivate
;
134 override public EntityType EntityType
138 return EntityType
.Property
;
142 public virtual IType Type
146 return _typeSystemServices
.Map(_memberInfo
.PropertyType
);
150 public System
.Reflection
.PropertyInfo PropertyInfo
158 public bool AcceptVarArgs
166 protected override Type MemberType
168 get { return _memberInfo.PropertyType; }
171 public virtual IParameter
[] GetParameters()
173 if (null != _parameters
) return _parameters
;
175 return _parameters
= _typeSystemServices
.Map(_memberInfo
.GetIndexParameters());
178 public virtual IMethod
GetGetMethod()
180 if (null != _getter
) return _getter
.Value
;
181 return (_getter
= new CachedMethod(FindGetMethod())).Value
;
184 private IMethod
FindGetMethod()
186 System
.Reflection
.MethodInfo getter
= _memberInfo
.GetGetMethod(true);
189 PropertyInfo baseProperty
= FindBaseProperty();
190 if (null == baseProperty
) return null;
192 getter
= baseProperty
.GetGetMethod(true);
193 if (null == getter
) return null;
195 return _typeSystemServices
.Map(getter
);
198 private PropertyInfo
FindBaseProperty()
200 return _memberInfo
.DeclaringType
.BaseType
.GetProperty(
202 _memberInfo
.PropertyType
,
203 GetParameterTypes(_memberInfo
.GetIndexParameters()));
206 private static Type
[] GetParameterTypes(ParameterInfo
[] parameters
)
208 Type
[] types
= new Type
[parameters
.Length
];
209 for (int i
=0; i
<parameters
.Length
; ++i
)
211 types
[i
] = parameters
[i
].ParameterType
;
216 public virtual IMethod
GetSetMethod()
218 if (null != _setter
) return _setter
.Value
;
219 return (_setter
= new CachedMethod(FindSetMethod())).Value
;
222 private IMethod
FindSetMethod()
224 System
.Reflection
.MethodInfo setter
= _memberInfo
.GetSetMethod(true);
225 if (null == setter
) return null;
226 return _typeSystemServices
.Map(setter
);
229 private System
.Reflection
.MethodInfo
GetAccessor()
231 if (null != _accessor
) return _accessor
;
233 return _accessor
= FindAccessor();
236 private System
.Reflection
.MethodInfo
FindAccessor()
238 System
.Reflection
.MethodInfo getter
= _memberInfo
.GetGetMethod(true);
239 if (null != getter
) return getter
;
240 return _memberInfo
.GetSetMethod(true);