public static ABInheritanceGuesserSummary GetSummary(List <InheritanceGuesserMatch> matches) { ABInheritanceGuesserSummary ret = new ABInheritanceGuesserSummary(); foreach (InheritanceGuesserMatch match in matches) { switch (match) { case InheritanceGuesserMatch.Perfect: ret.TotalAmountPerfect += 1; break; case InheritanceGuesserMatch.NoCorrelation: ret.CompleteOff += 1; ret.TotalAmountOff += 1; break; case InheritanceGuesserMatch.NameOff: ret.NamesOff += 1; ret.TotalAmountOff += 1; break; case InheritanceGuesserMatch.TypeOff: ret.TypesOff += 1; ret.TotalAmountOff += 1; break; } } return(ret); }
internal static Type GetBestInheritanceInternal(Type baseType, List <ABInheritanceGuesserObject> objects) { // Get all the avalible types MethodInfo method = typeof(ABInheritanceGuesser).GetMethod("GetAllInheriters") .MakeGenericMethod(new Type[] { baseType }); dynamic inheriters = method.Invoke(typeof(ABInheritanceGuesser), new object[] { }); int i = 0; List <ABInheritanceGuesserSummary> summaries = new List <ABInheritanceGuesserSummary>(); foreach (object obj in inheriters) { List <InheritanceGuesserMatch> matches = CompareClassAgainstObjects(obj, objects); ABInheritanceGuesserSummary summary = GetSummary(matches); summaries.Add(summary); if (summaries[i].TotalAmountOff == 0 && summaries[i].TotalAmountPerfect > 0) { return(obj.GetType()); // This means it is perfect - same amount of types and everything! Use this! } i++; } // If we are still here it means none of the types were "perfect" and we have to determine the best one out of what we have. int a = 0; Type BestType = null; ABInheritanceGuesserSummary BestSummary = new ABInheritanceGuesserSummary(); foreach (object obj in inheriters) { if (BestType == null) { BestType = obj.GetType(); BestSummary = summaries[a]; } bool TypeWorse = false; bool NameWorse = false; if (summaries[a].TypesOff > BestSummary.TypesOff) { TypeWorse = true; } if (summaries[a].NamesOff > BestSummary.NamesOff) { NameWorse = true; } bool passed = false; if (NameWorse) { if (!TypeWorse) { if (summaries[a].CompleteOff < BestSummary.CompleteOff) // If the best one is worse! { passed = true; } } } else { passed = true; } if (passed) { if (!TypeWorse) { BestType = obj.GetType(); BestSummary = summaries[a]; } } a++; } if (BestType != null) { return(BestType); } return(baseType); // If it failed to return - it means that none of the inherited types are working, so it must be the original here. }