Merge pull request #733 from amoiseev-softheme/bugfix/monofix
[mono.git] / mcs / class / corlib / System.Reflection / TypeInfo.cs
1 //
2 // TypeInfo.cs
3 //
4 // Authors:
5 //    Marek Safar  <marek.safar@gmail.com>
6 //    Martin Baulig <martin.baulig@xamarin.com>
7 //
8 // Copyright (c) 2011-2013 Xamarin Inc. (http://www.xamarin.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 // THE SOFTWARE.
27
28 #if NET_4_5
29 using System.Collections.Generic;
30
31 namespace System.Reflection
32 {
33         public abstract class TypeInfo : Type, IReflectableType
34         {
35                 internal TypeInfo ()
36                 {
37                 }
38                 
39                 TypeInfo IReflectableType.GetTypeInfo ()
40                 {
41                         return this;
42                 }
43
44                 static readonly BindingFlags declaredFlags = BindingFlags.DeclaredOnly |
45                         BindingFlags.Public | BindingFlags.NonPublic |
46                                 BindingFlags.Static | BindingFlags.Instance;
47                 
48                 public virtual IEnumerable<ConstructorInfo> DeclaredConstructors {
49                         get { return GetConstructors (declaredFlags); }
50                 }
51                 
52                 public virtual IEnumerable<EventInfo> DeclaredEvents {
53                         get { return GetEvents (declaredFlags); }
54                 }
55                 
56                 public virtual IEnumerable<FieldInfo> DeclaredFields {
57                         get { return GetFields (declaredFlags); }
58                 }
59                 
60                 public virtual IEnumerable<MethodInfo> DeclaredMethods {
61                         get { return GetMethods (declaredFlags); }
62                 }
63                 
64                 public virtual IEnumerable<PropertyInfo> DeclaredProperties {
65                         get { return GetProperties (declaredFlags); }
66                 }
67                 
68                 public virtual IEnumerable<MemberInfo> DeclaredMembers {
69                         get {
70                                 var ret = new List<MemberInfo> ();
71                                 ret.AddRange (DeclaredConstructors);
72                                 ret.AddRange (DeclaredEvents);
73                                 ret.AddRange (DeclaredFields);
74                                 ret.AddRange (DeclaredMethods);
75                                 ret.AddRange (DeclaredProperties);
76                                 return ret.AsReadOnly ();
77                         }
78                 }
79                 
80                 public virtual IEnumerable<TypeInfo> DeclaredNestedTypes {
81                         get {
82                                 foreach (var nested in GetNestedTypes (declaredFlags))
83                                         yield return new TypeDelegator (nested);
84                         }
85                 }
86                 
87                 public virtual Type[] GenericTypeParameters {
88                         get {
89                                 if (!ContainsGenericParameters)
90                                         return new Type [0];
91                                 return GetGenericArguments ();
92                         }
93                 }
94
95                 static bool list_contains (IEnumerable<Type> types, Type type)
96                 {
97                         foreach (var t in types) {
98                                 if (t == type)
99                                         return true;
100                         }
101
102                         return false;
103                 }
104                 
105                 public virtual IEnumerable<Type> ImplementedInterfaces {
106                         get {
107                                 var all = GetInterfaces ();
108                                 var baseIfaces = BaseType != null ? BaseType.GetInterfaces () : new Type [0];
109                                 foreach (var iface in all)
110                                         if (!list_contains (baseIfaces, iface))
111                                                 yield return iface;
112                         }
113                 }
114
115                 public virtual Type AsType ()
116                 {
117                         return this;
118                 }
119
120                 public virtual EventInfo GetDeclaredEvent (string name)
121                 {
122                         return GetEvent (name, declaredFlags);
123                 }
124
125                 public virtual FieldInfo GetDeclaredField (string name)
126                 {
127                         return GetField (name, declaredFlags);
128                 }
129
130                 public virtual MethodInfo GetDeclaredMethod (string name)
131                 {
132                         return GetMethod (name, declaredFlags);
133                 }
134
135                 public virtual IEnumerable<MethodInfo> GetDeclaredMethods (string name)
136                 {
137                         foreach (var method in GetMethods (declaredFlags))
138                                 if (method.Name.Equals (name))
139                                         yield return method;
140                 }
141
142                 public virtual TypeInfo GetDeclaredNestedType (string name)
143                 {
144                         var nested = GetNestedType (name, declaredFlags);
145                         if (nested != null)
146                                 return new TypeDelegator (nested);
147                         else
148                                 return null;
149                 }
150
151                 public virtual PropertyInfo GetDeclaredProperty (string name)
152                 {
153                         return GetProperty (name, declaredFlags);
154                 }
155
156                 public virtual bool IsAssignableFrom (TypeInfo typeInfo)
157                 {
158                         return IsAssignableFrom (typeInfo.AsType ());
159                 }
160         }
161 }
162 #endif