Facilitate the merge
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Scripting.Core / Utils / TypeExtensions.cs
1 /* ****************************************************************************
2  *
3  * Copyright (c) Microsoft Corporation. 
4  *
5  * This source code is subject to terms and conditions of the Microsoft Public License. A 
6  * copy of the license can be found in the License.html file at the root of this distribution. If 
7  * you cannot locate the  Microsoft Public License, please send an email to 
8  * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
9  * by the terms of the Microsoft Public License.
10  *
11  * You must not remove this notice, or any other, from this software.
12  *
13  *
14  * ***************************************************************************/
15
16 using System.Diagnostics;
17 using System.Reflection;
18 using System.Reflection.Emit;
19 using System.Text;
20
21 namespace System.Dynamic.Utils {
22
23     // Extensions on System.Type and friends
24     internal static class TypeExtensions {
25
26         /// <summary>
27         /// Creates an open delegate for the given (dynamic)method.
28         /// </summary>
29         internal static Delegate CreateDelegate(this MethodInfo methodInfo, Type delegateType) {
30             Debug.Assert(methodInfo != null && delegateType != null);
31
32             var dm = methodInfo as DynamicMethod;
33             if (dm != null) {
34                 return dm.CreateDelegate(delegateType);
35             } else {
36                 return Delegate.CreateDelegate(delegateType, methodInfo);
37             }
38         }
39
40         /// <summary>
41         /// Creates a closed delegate for the given (dynamic)method.
42         /// </summary>
43         internal static Delegate CreateDelegate(this MethodInfo methodInfo, Type delegateType, object target) {
44             Debug.Assert(methodInfo != null && delegateType != null);
45
46             var dm = methodInfo as DynamicMethod;
47             if (dm != null) {
48                 return dm.CreateDelegate(delegateType, target);
49             } else {
50                 return Delegate.CreateDelegate(delegateType, target, methodInfo);
51             }
52         }
53
54         internal static Type GetReturnType(this MethodBase mi) {
55             return (mi.IsConstructor) ? mi.DeclaringType : ((MethodInfo)mi).ReturnType;
56         }
57
58         private static readonly CacheDict<MethodBase, ParameterInfo[]> _ParamInfoCache = new CacheDict<MethodBase, ParameterInfo[]>(75);
59         
60         internal static ParameterInfo[] GetParametersCached(this MethodBase method) {
61             ParameterInfo[] pis;
62             lock (_ParamInfoCache) {
63                 if (!_ParamInfoCache.TryGetValue(method, out pis)) {
64                     pis = method.GetParameters();
65
66                     Type t = method.DeclaringType;
67                     if (t != null && TypeUtils.CanCache(t)) {
68                         _ParamInfoCache[method] = pis;
69                     }
70                 }
71             }
72             return pis;
73         }
74
75         // Expression trees/compiler just use IsByRef, why do we need this?
76         // (see LambdaCompiler.EmitArguments for usage in the compiler)
77         internal static bool IsByRefParameter(this ParameterInfo pi) {
78             // not using IsIn/IsOut properties as they are not available in Silverlight:
79             if (pi.ParameterType.IsByRef) return true;
80
81             return (pi.Attributes & (ParameterAttributes.Out)) == ParameterAttributes.Out;
82         }
83
84         // Returns the matching method if the parameter types are reference
85         // assignable from the provided type arguments, otherwise null. 
86         internal static MethodInfo GetMethodValidated(
87             this Type type,
88             string name,
89             BindingFlags bindingAttr,
90             Binder binder,
91             Type[] types,
92             ParameterModifier[] modifiers) {
93             
94             var method = type.GetMethod(name, bindingAttr, binder, types, modifiers);
95
96             return method.MatchesArgumentTypes(types) ? method : null;
97         }
98
99         /// <summary>
100         /// Returns true if the method's parameter types are reference assignable from
101         /// the argument types, otherwise false.
102         /// 
103         /// An example that can make the method return false is that 
104         /// typeof(double).GetMethod("op_Equality", ..., new[] { typeof(double), typeof(int) })
105         /// returns a method with two double parameters, which doesn't match the provided
106         /// argument types.
107         /// </summary>
108         /// <returns></returns>
109         private static bool MatchesArgumentTypes(this MethodInfo mi, Type[] argTypes) {
110             if (mi == null || argTypes == null) {
111                 return false;
112             }
113             var ps = mi.GetParameters();
114
115             if (ps.Length != argTypes.Length) {
116                 return false;
117             }
118
119             for (int i = 0; i < ps.Length; i++) {
120                 if (!TypeUtils.AreReferenceAssignable(ps[i].ParameterType, argTypes[i])) {
121                     return false;
122                 }
123             }
124             return true;
125         }
126     }
127 }