diff --git a/src/Orchard.Tests/Commands/CommandHandlerDescriptorBuilderTests.cs b/src/Orchard.Tests/Commands/CommandHandlerDescriptorBuilderTests.cs index a3e2ca821..e168c69f8 100644 --- a/src/Orchard.Tests/Commands/CommandHandlerDescriptorBuilderTests.cs +++ b/src/Orchard.Tests/Commands/CommandHandlerDescriptorBuilderTests.cs @@ -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 Event; // no event adder, remover, etc. + + // no private method + private void Blah() { + } + + public void Method() { + } + } + } } diff --git a/src/Orchard/Commands/CommandHandlerDescriptorBuilder.cs b/src/Orchard/Commands/CommandHandlerDescriptorBuilder.cs index 499e55775..4f17a1a9f 100644 --- a/src/Orchard/Commands/CommandHandlerDescriptorBuilder.cs +++ b/src/Orchard/Commands/CommandHandlerDescriptorBuilder.cs @@ -10,11 +10,36 @@ namespace Orchard.Commands { } private IEnumerable 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 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),