public void DistanceTest() { Type[] types1 = new Type[] { typeof(object), typeof(int), typeof(float), typeof(Action), typeof(List<int>) }; Type[] types2 = new Type[] { typeof(object), typeof(int), typeof(float), typeof(Delegate), typeof(IList<int>) }; Assert.AreEqual(types1.Distance(types2), 2); Assert.AreEqual(types2.Distance(types1), 2); Assert.AreEqual(types1.Distance(types1), 0); Signature s1 = new Signature() { ParameterTypes = types1, ReturnType = typeof(Int16) }; Signature s2 = new Signature() { ParameterTypes = types2, ReturnType = typeof(object) }; Assert.AreEqual(s1.Distance(s2), 3); Assert.AreEqual(s2.Distance(s1), 3); Assert.AreEqual(s1.Distance(s1), 0); s1 = new Signature() { ParameterTypes = types1, ReturnType = typeof(Int16) }; s2 = new Signature() { ParameterTypes = types2, ReturnType = typeof(Int16) }; Assert.AreEqual(s1.Distance(s2), 2); Assert.AreEqual(s2.Distance(s1), 2); }
public MethodInfo GetMethodByParamsAndReturnValueType(Type ReturnType, object[] args) { Signature sig = new Signature(); if (args == null) args = new object[0]; if (ReturnType != null) sig.ReturnType = ReturnType; else sig.ReturnType = typeof(void); sig.ParameterTypes = args.GetTypes(); var t1 = MethodsBySignature.Keys.Where( (s) => { return s.CanStore(sig); }).Select( (s) => { return MethodsBySignature[s]; }); if (t1.Count() == 0) return null; var t5 = t1.Aggregate( (a, b) => { return a.Concat(b).ToArray(); }); var t2 = t5.Where( (m) => { var tup = BeRestrictions[m]; return tup.Item1.Length == 0 ? true : tup.Item1.Select((b, i) => { return !b ? true : (RestrPolar[new Tuple<MethodInfo, int>(m, i)] ? Restrictions.ContainsKey( new Tuple<MethodInfo, int, object>(m, i, args[i])) : !Restrictions.ContainsKey( new Tuple<MethodInfo, int, object>(m, i, args[i]))); }).Aggregate((a, b) => { return a && b; }); }).OrderByDescending( (m) => { return BeRestrictions[m].Item2; }); var t3 = t2.GroupBy( (m) => { return BeRestrictions[m].Item2; }); var t4 = t3.First().ToArray(). OrderBy( (m) => { int dist = sig.Distance(m.GetSignature()); return dist; }). FirstOrDefault(); return t4; }