Merge pull request #293 from viniciusjarina/fix3258
[mono.git] / mcs / class / IKVM.Reflection / ParameterInfo.cs
1 /*
2   Copyright (C) 2009 Jeroen Frijters
3
4   This software is provided 'as-is', without any express or implied
5   warranty.  In no event will the authors be held liable for any damages
6   arising from the use of this software.
7
8   Permission is granted to anyone to use this software for any purpose,
9   including commercial applications, and to alter it and redistribute it
10   freely, subject to the following restrictions:
11
12   1. The origin of this software must not be misrepresented; you must not
13      claim that you wrote the original software. If you use this software
14      in a product, an acknowledgment in the product documentation would be
15      appreciated but is not required.
16   2. Altered source versions must be plainly marked as such, and must not be
17      misrepresented as being the original software.
18   3. This notice may not be removed or altered from any source distribution.
19
20   Jeroen Frijters
21   jeroen@frijters.net
22   
23 */
24 using System.Collections.Generic;
25
26 namespace IKVM.Reflection
27 {
28         public abstract class ParameterInfo : ICustomAttributeProvider
29         {
30                 // prevent external subclasses
31                 internal ParameterInfo()
32                 {
33                 }
34
35                 public sealed override bool Equals(object obj)
36                 {
37                         ParameterInfo other = obj as ParameterInfo;
38                         return other != null && other.Member == this.Member && other.Position == this.Position;
39                 }
40
41                 public sealed override int GetHashCode()
42                 {
43                         return this.Member.GetHashCode() * 1777 + this.Position;
44                 }
45
46                 public static bool operator ==(ParameterInfo p1, ParameterInfo p2)
47                 {
48                         return ReferenceEquals(p1, p2) || (!ReferenceEquals(p1, null) && p1.Equals(p2));
49                 }
50
51                 public static bool operator !=(ParameterInfo p1, ParameterInfo p2)
52                 {
53                         return !(p1 == p2);
54                 }
55
56                 public abstract string Name { get; }
57                 public abstract Type ParameterType { get; }
58                 public abstract ParameterAttributes Attributes { get; }
59                 public abstract int Position { get; }
60                 public abstract object RawDefaultValue { get; }
61                 public abstract CustomModifiers __GetCustomModifiers();
62                 public abstract MemberInfo Member { get; }
63                 public abstract int MetadataToken { get; }
64                 internal abstract Module Module { get; }
65
66                 public Type[] GetOptionalCustomModifiers()
67                 {
68                         return __GetCustomModifiers().GetOptional();
69                 }
70
71                 public Type[] GetRequiredCustomModifiers()
72                 {
73                         return __GetCustomModifiers().GetRequired();
74                 }
75
76                 public bool IsIn
77                 {
78                         get { return (Attributes & ParameterAttributes.In) != 0; }
79                 }
80
81                 public bool IsOut
82                 {
83                         get { return (Attributes & ParameterAttributes.Out) != 0; }
84                 }
85
86                 public bool IsLcid
87                 {
88                         get { return (Attributes & ParameterAttributes.Lcid) != 0; }
89                 }
90
91                 public bool IsRetval
92                 {
93                         get { return (Attributes & ParameterAttributes.Retval) != 0; }
94                 }
95
96                 public bool IsOptional
97                 {
98                         get { return (Attributes & ParameterAttributes.Optional) != 0; }
99                 }
100
101                 public bool IsDefined(Type attributeType, bool inherit)
102                 {
103                         return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit).Count != 0;
104                 }
105
106                 public IList<CustomAttributeData> __GetCustomAttributes(Type attributeType, bool inherit)
107                 {
108                         return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit);
109                 }
110
111                 internal virtual IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
112                 {
113                         return this.Module.GetCustomAttributes(this.MetadataToken, attributeType);
114                 }
115         }
116
117         sealed class ParameterInfoWrapper : ParameterInfo
118         {
119                 private readonly MemberInfo member;
120                 private readonly ParameterInfo forward;
121
122                 internal ParameterInfoWrapper(MemberInfo member, ParameterInfo forward)
123                 {
124                         this.member = member;
125                         this.forward = forward;
126                 }
127
128                 public override string Name
129                 {
130                         get { return forward.Name; }
131                 }
132
133                 public override Type ParameterType
134                 {
135                         get { return forward.ParameterType; }
136                 }
137
138                 public override ParameterAttributes Attributes
139                 {
140                         get { return forward.Attributes; }
141                 }
142
143                 public override int Position
144                 {
145                         get { return forward.Position; }
146                 }
147
148                 public override object RawDefaultValue
149                 {
150                         get { return forward.RawDefaultValue; }
151                 }
152
153                 public override CustomModifiers __GetCustomModifiers()
154                 {
155                         return forward.__GetCustomModifiers();
156                 }
157
158                 public override MemberInfo Member
159                 {
160                         get { return member; }
161                 }
162
163                 public override int MetadataToken
164                 {
165                         get { return forward.MetadataToken; }
166                 }
167
168                 internal override Module Module
169                 {
170                         get { return member.Module; }
171                 }
172
173                 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
174                 {
175                         return forward.GetCustomAttributesData(attributeType);
176                 }
177         }
178 }