Created
June 4, 2012 15:25
-
-
Save anonymous/2869036 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <Name>Do not hide base class methods</Name> | |
// Prefer override virtual methods | |
// More on hiding vs. virtual usefulness here: | |
// http://www.artima.com/intv/nonvirtual.html | |
// http://blogs.msdn.com/b/ericlippert/archive/2008/05/21/method-hiding-apologia.aspx | |
warnif count > 0 | |
// Define lookup table indexing methods by their name including parameters signature. | |
let lookup = Methods.Where(m => !m.IsConstructor && !m.IsStatic && !m.IsGeneratedByCompiler) | |
.ToLookup(m1 => m1.Name) | |
from t in Application.Types | |
where !t.IsStatic && t.IsClass && | |
// Discard classes deriving directly from System.Object | |
t.DepthOfInheritance > 1 | |
where t.BaseClasses.Any() | |
// For each methods not overriding any methods (new slot), | |
// let's check if it hides by name some methods defined in base classe. | |
from m in t.InstanceMethods | |
where m.IsNewSlot && !m.IsExplicitInterfaceImpl && !m.IsGeneratedByCompiler | |
// Notice how lookup is used to quickly retrieve methods with same name as m. | |
// This makes the query 10 times faster than iterating each base methods to check their name. | |
let baseMethodsHidden = lookup[m.Name].Where(m1 => m1 != m && t.DeriveFrom(m1.ParentType)) | |
where baseMethodsHidden.Count() > 0 | |
select new { m, baseMethodsHidden } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment