Fix CommandHandlerDescriptorBuilder to return only public methods

--HG--
branch : dev
This commit is contained in:
Renaud Paquay 2010-04-26 18:11:13 -07:00
parent d6b5a73783
commit 4dbaa57f2c
2 changed files with 49 additions and 1 deletions

View File

@ -39,5 +39,28 @@ namespace Orchard.Tests.Commands {
public void Foo_Bar3() {
}
}
[Test]
public void BuilderShouldReturnPublicMethodsOnly() {
var builder = new CommandHandlerDescriptorBuilder();
var descriptor = builder.Build(typeof(PublicMethodsOnly));
Assert.That(descriptor, Is.Not.Null);
Assert.That(descriptor.Commands.Count(), Is.EqualTo(1));
Assert.That(descriptor.Commands.Single(d => d.Name == "Method"), Is.Not.Null);
}
public class PublicMethodsOnly {
public bool Bar { get; set; } // no accessors
public bool Field = true; // no field
public event Action<int> Event; // no event adder, remover, etc.
// no private method
private void Blah() {
}
public void Method() {
}
}
}
}

View File

@ -10,11 +10,36 @@ namespace Orchard.Commands {
}
private IEnumerable<CommandDescriptor> CollectMethods(Type type) {
foreach (var methodInfo in type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)) {
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);
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),