Add unit test for AggregateException.GetBaseException that works on .net but is broke...
[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 Apache License, Version 2.0. 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  Apache License, Version 2.0, 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 Apache License, Version 2.0.
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 a closed delegate for the given (dynamic)method.
28         /// </summary>
29         internal static Delegate CreateDelegate(this MethodInfo methodInfo, Type delegateType, object target) {
30             Debug.Assert(methodInfo != null && delegateType != null);
31
32             var dm = methodInfo as DynamicMethod;
33             if (dm != null) {
34                 return dm.CreateDelegate(delegateType, target);
35             } else {
36                 return Delegate.CreateDelegate(delegateType, target, methodInfo);
37             }
38         }
39
40         internal static Type GetReturnType(this MethodBase mi) {
41             return (mi.IsConstructor) ? mi.DeclaringType : ((MethodInfo)mi).ReturnType;
42         }
43
44         private static readonly CacheDict<MethodBase, ParameterInfo[]> _ParamInfoCache = new CacheDict<MethodBase, ParameterInfo[]>(75);
45         
46         internal static ParameterInfo[] GetParametersCached(this MethodBase method) {
47             ParameterInfo[] pis;
48             lock (_ParamInfoCache) {
49                 if (!_ParamInfoCache.TryGetValue(method, out pis)) {
50                     pis = method.GetParameters();
51
52                     Type t = method.DeclaringType;
53                     if (t != null && TypeUtils.CanCache(t)) {
54                         _ParamInfoCache[method] = pis;
55                     }
56                 }
57             }
58             return pis;
59         }
60
61         // Expression trees/compiler just use IsByRef, why do we need this?
62         // (see LambdaCompiler.EmitArguments for usage in the compiler)
63         internal static bool IsByRefParameter(this ParameterInfo pi) {
64             // not using IsIn/IsOut properties as they are not available in Silverlight:
65             if (pi.ParameterType.IsByRef) return true;
66
67             return (pi.Attributes & (ParameterAttributes.Out)) == ParameterAttributes.Out;
68         }
69
70         // Returns the matching method if the parameter types are reference
71         // assignable from the provided type arguments, otherwise null. 
72         internal static MethodInfo GetMethodValidated(
73             this Type type,
74             string name,
75             BindingFlags bindingAttr,
76             Binder binder,
77             Type[] types,
78             ParameterModifier[] modifiers) {
79             
80             var method = type.GetMethod(name, bindingAttr, binder, types, modifiers);
81
82             return method.MatchesArgumentTypes(types) ? method : null;
83         }
84
85         /// <summary>
86         /// Returns true if the method's parameter types are reference assignable from
87         /// the argument types, otherwise false.
88         /// 
89         /// An example that can make the method return false is that 
90         /// typeof(double).GetMethod("op_Equality", ..., new[] { typeof(double), typeof(int) })
91         /// returns a method with two double parameters, which doesn't match the provided
92         /// argument types.
93         /// </summary>
94         /// <returns></returns>
95         private static bool MatchesArgumentTypes(this MethodInfo mi, Type[] argTypes) {
96             if (mi == null || argTypes == null) {
97                 return false;
98             }
99             var ps = mi.GetParameters();
100
101             if (ps.Length != argTypes.Length) {
102                 return false;
103             }
104
105             for (int i = 0; i < ps.Length; i++) {
106                 if (!TypeUtils.AreReferenceAssignable(ps[i].ParameterType, argTypes[i])) {
107                     return false;
108                 }
109             }
110             return true;
111         }
112     }
113 }