1 // ***********************************************************************
2 // Copyright (c) 2015 Charlie Poole
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
30 using System.Collections.Generic;
31 using System.Reflection;
34 namespace NUnit.Compatibility
37 /// Type extensions that apply to all target frameworks
39 public static class AdditionalTypeExtensions
42 /// Determines if the given <see cref="Type"/> array is castable/matches the <see cref="ParameterInfo"/> array.
44 /// <param name="pinfos"></param>
45 /// <param name="ptypes"></param>
46 /// <returns></returns>
47 public static bool ParametersMatch(this ParameterInfo[] pinfos, Type[] ptypes)
49 if (pinfos.Length != ptypes.Length)
52 for (int i = 0; i < pinfos.Length; i++)
54 if (!pinfos[i].ParameterType.IsCastableFrom(ptypes[i]))
60 // §6.1.2 (Implicit numeric conversions) of the specification
61 static Dictionary<Type, List<Type>> convertibleValueTypes = new Dictionary<Type, List<Type>>() {
62 { typeof(decimal), new List<Type> { typeof(sbyte), typeof(byte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(char) } },
63 { typeof(double), new List<Type> { typeof(sbyte), typeof(byte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(char), typeof(float) } },
64 { typeof(float), new List<Type> { typeof(sbyte), typeof(byte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(char), typeof(float) } },
65 { typeof(ulong), new List<Type> { typeof(byte), typeof(ushort), typeof(uint), typeof(char) } },
66 { typeof(long), new List<Type> { typeof(sbyte), typeof(byte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(char) } },
67 { typeof(uint), new List<Type> { typeof(byte), typeof(ushort), typeof(char) } },
68 { typeof(int), new List<Type> { typeof(sbyte), typeof(byte), typeof(short), typeof(ushort), typeof(char) } },
69 { typeof(ushort), new List<Type> { typeof(byte), typeof(char) } },
70 { typeof(short), new List<Type> { typeof(byte) } }
74 /// Determines if one type can be implicitly converted from another
76 /// <param name="to"></param>
77 /// <param name="from"></param>
78 /// <returns></returns>
79 public static bool IsCastableFrom(this Type to, Type from)
81 if (to.IsAssignableFrom(from))
84 // Look for the marker that indicates from was null
85 if (from == typeof(NUnitNullType) && (to.GetTypeInfo().IsClass || to.FullName.StartsWith("System.Nullable")))
88 if (convertibleValueTypes.ContainsKey(to) && convertibleValueTypes[to].Contains(from))
91 return from.GetMethods(BindingFlags.Public | BindingFlags.Static)
92 .Any(m => m.ReturnType == to && m.Name == "op_Implicit");
97 /// This class is used as a flag when we get a parameter list for a method/constructor, but
98 /// we do not know one of the types because null was passed in.
100 public class NUnitNullType