Simplify code to filter out special methods

--HG--
branch : dev
This commit is contained in:
Renaud Paquay
2010-04-27 14:44:55 -07:00
parent 0f93e120c3
commit 03859bffd8
2 changed files with 15 additions and 24 deletions

View File

@@ -58,6 +58,18 @@ namespace Orchard.Tests.Commands {
private void Blah() {
}
// no private method
public static void Foo() {
}
// no operator
public static bool operator ==(PublicMethodsOnly a, PublicMethodsOnly b) {
return false;
}
public static bool operator !=(PublicMethodsOnly a, PublicMethodsOnly b) {
return false;
}
public void Method() {
}
}

View File

@@ -10,36 +10,15 @@ namespace Orchard.Commands {
}
private IEnumerable<CommandDescriptor> CollectMethods(Type type) {
var allMethods = type
.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
var allAccessors = type
.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)
.SelectMany(p => p.GetAccessors());
var allEventMethods = type
.GetEvents(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)
.SelectMany(e => GetEventMethods(e));
var methods = allMethods.Except(allAccessors).Except(allEventMethods);
var methods = type
.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)
.Where(m => !m.IsSpecialName);
foreach (var methodInfo in methods) {
yield return BuildMethod(methodInfo);
}
}
private IEnumerable<MethodInfo> GetEventMethods(EventInfo info) {
if (info.GetAddMethod() != null)
yield return info.GetAddMethod();
if (info.GetRaiseMethod() != null)
yield return info.GetRaiseMethod();
if (info.GetRemoveMethod() != null)
yield return info.GetRemoveMethod();
foreach(var other in info.GetOtherMethods())
yield return other;
}
private CommandDescriptor BuildMethod(MethodInfo methodInfo) {
return new CommandDescriptor {
Name = GetCommandName(methodInfo),