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
31 using System.Collections.Generic;
34 using System.Reflection;
35 using NUnit.Framework.Interfaces;
37 namespace NUnit.Framework.Internal
39 static class NetCFExtensions
41 public static IMethodInfo MakeGenericMethodEx(this IMethodInfo method, object[] arguments)
43 var newMi = method.MethodInfo.MakeGenericMethodEx(arguments);
44 if (newMi == null) return null;
46 return new MethodWrapper(method.TypeInfo.Type, newMi);
49 public static MethodInfo MakeGenericMethodEx(this MethodInfo mi, object[] arguments)
51 return mi.MakeGenericMethodEx(arguments.Select(a => a == null ? typeof(object) : (a is Type ? typeof(Type) : a.GetType())).ToArray());
54 public static MethodInfo MakeGenericMethodEx(this MethodInfo mi, Type[] types)
56 if(!mi.ContainsGenericParameters)
59 if(!mi.IsGenericMethodDefinition)
60 return mi.MakeGenericMethod(types);
62 var args = mi.GetGenericArguments();
64 if(args.Length == types.Length)
65 return mi.MakeGenericMethod(types);
67 if(args.Length > types.Length)
70 var tai = new TypeArrayIterator(types, args.Length);
72 foreach(var ta in tai)
74 var newMi = mi.MakeGenericMethod(ta);
75 var pa = newMi.GetParameters();
76 if(types.SequenceEqual(pa.Select(p => p.ParameterType)))
80 foreach(var ta in tai)
82 var newMi = mi.MakeGenericMethod(ta);
83 var pa = newMi.GetParameters();
84 if(types.Select((t,ix) => (Type)TypeHelper.BestCommonType(t, pa[ix].ParameterType)).SequenceEqual(pa.Select(p => p.ParameterType)))
93 internal class TypeArrayIterator : IEnumerable<Type[]>
95 private Type[] _types;
96 private int _neededLength;
98 public TypeArrayIterator(Type[] types, int neededLength)
101 _neededLength = neededLength;
104 #region IEnumerable<CType[]> Members
106 public IEnumerator<Type[]> GetEnumerator()
108 var indices = new int[_neededLength];
111 var results = new List<Type> ();
113 for(int i = 0; i < _neededLength; ++i)
114 results.Add(_types[indices[i]]);
116 if(indices.Distinct().Count() == _neededLength)
117 yield return results.ToArray();
119 for(int j = _neededLength - 1; j >= 0; --j)
121 if(++indices[j] < _types.Length)
134 #region IEnumerable Members
136 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
138 return GetEnumerator();