Merge pull request #799 from kebby/master
[mono.git] / mcs / class / corlib / System.Reflection / RuntimeReflectionExtensions.cs
1 //
2 // RuntimeReflectionExtensions.cs
3 //
4 // Authors:
5 //       Martin Baulig <martin.baulig@xamarin.com>
6 //       Marek Safar (marek.safar@gmail.com)
7 //
8 // Copyright (c) 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
30 using System.Collections.Generic;
31
32 namespace System.Reflection
33 {
34         public static class RuntimeReflectionExtensions
35         {
36                 const BindingFlags AllMembersBindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
37
38                 public static MethodInfo GetMethodInfo (this Delegate del)
39                 {
40                         if (del == null)
41                                 throw new ArgumentNullException ("del");
42
43                         return del.Method;
44                 }
45
46                 public static MethodInfo GetRuntimeBaseDefinition (this MethodInfo method)
47                 {
48                         if (method == null)
49                                 throw new ArgumentNullException ("method");
50
51                         return method.GetBaseDefinition ();
52                 }
53
54                 public static EventInfo GetRuntimeEvent (this Type type, string name)
55                 {
56                         if (type == null)
57                                 throw new ArgumentNullException ("type");
58
59                         return type.GetEvent (name);
60                 }
61
62                 public static IEnumerable<EventInfo> GetRuntimeEvents (this Type type)
63                 {
64                         if (type == null)
65                                 throw new ArgumentNullException ("type");
66
67                         return type.GetEvents (AllMembersBindingFlags);
68                 }
69
70                 public static FieldInfo GetRuntimeField (this Type type, string name)
71                 {
72                         if (type == null)
73                                 throw new ArgumentNullException ("type");
74
75                         return type.GetField (name);
76                 }
77
78                 public static IEnumerable<FieldInfo> GetRuntimeFields (this Type type)
79                 {
80                         if (type == null)
81                                 throw new ArgumentNullException ("type");
82
83                         return type.GetFields (AllMembersBindingFlags);
84                 }
85
86                 public static InterfaceMapping GetRuntimeInterfaceMap (this TypeInfo typeInfo, Type interfaceType)
87                 {
88                         if (typeInfo == null)
89                                 throw new ArgumentNullException ("typeInfo");
90
91                         return typeInfo.GetInterfaceMap (interfaceType);
92                 }
93
94                 public static MethodInfo GetRuntimeMethod (this Type type, string name, Type[] parameters)
95                 {
96                         if (type == null)
97                                 throw new ArgumentNullException ("type");
98
99                         return type.GetMethod (name, parameters);
100                 }
101
102                 public static IEnumerable<MethodInfo> GetRuntimeMethods (this Type type)
103                 {
104                         if (type == null)
105                                 throw new ArgumentNullException ("type");
106
107                         return type.GetMethods (AllMembersBindingFlags);
108                 }
109
110                 public static IEnumerable<PropertyInfo> GetRuntimeProperties (this Type type)
111                 {
112                         if (type == null)
113                                 throw new ArgumentNullException ("type");
114
115                         return type.GetProperties (AllMembersBindingFlags);
116                 }
117
118                 public static PropertyInfo GetRuntimeProperty (this Type type, string name)
119                 {
120                         if (type == null)
121                                 throw new ArgumentNullException ("type");
122
123                         return type.GetProperty (name);
124                 }
125         }
126 }
127 #endif
128