实施 CommandFactory 解决方案问题

Implementing CommandFactory solution issue

我正在尝试从另一个 SO post here 实施解决方案,以便能够直接编写命令代码,而不是使用 SRGMS XML 文件作为命令输入。我相信这就是 post 正在尝试做的事情,但我在实施它时遇到了麻烦。

我的代码在下面,但它不起作用(错误行旁边有注释)。如果有人可以提供有关如何使此代码在 WindowsForm 应用程序中工作的简单解释,我将不胜感激。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Speech.Recognition;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Commands
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        LoadStart();
    }

    private void LoadStart()
    {
        MethodInfo myMethod;

        var methods = new Commands();

        myMethod = CommandFactory.GetCommandMethods("Time Please");
        myMethod.Invoke(methods, null);

        myMethod = CommandFactory.GetCommandMethods("Volume Down");
        myMethod.Invoke(methods, null);

        myMethod = CommandFactory.GetCommandMethods("Volume Up");
        myMethod.Invoke(methods, null);
    }

    public static class CommandFactory
    {
        private static Dictionary<string, MethodInfo> commandMethods = new Dictionary<string, MethodInfo>();

        public static MethodInfo GetCommandMethods(string Command)
        {
            MethodInfo methodInfo;

            var myCommandMethods = new Commands();

            if (commandMethods.Count == 0)
            {
                var methodNames = typeof(Commands).GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance);

                var speechAttributeMethods = methodNames.Where(y => y.GetCustomAttributes().OfType<CommandAttribute>().Any());

                foreach (var speechAttributeMethod in speechAttributeMethods)
                {
                    foreach (var attribute in speechAttributeMethod.GetCustomAttributes(true))
                    {
                        commandMethods.Add(((CommandAttribute)attribute).CommandValue, speechAttributeMethod);
                    }
                }
                methodInfo = commandMethods[Command];
            }
            else
            {
                methodInfo = commandMethods[Command];
            }

            return methodInfo;
        }

        public class Commands
        {
            [Command("Time Please")]
            [Command("Whats the Time")]
            [Command("What Time is it")]
            public void GetTime()
            {
                Console.WriteLine(DateTime.Now.ToLocalTime());
            }

            [Command("Volume Down")]
            public void VolumeDown()
            {
                Console.WriteLine("Volume Down 1");
            }

            [Command("Volume Up")]
            public void VolumeUp()
            {
                Console.WriteLine("Volume Up 1");
            }
        }

        [System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple = true)]
        public class CommandAttribute : System.Attribute
        {
            public string CommandValue { get; set; }

            public CommandAttribute(string textValue)
            {
                this.CommandValue = textValue;
            }
        }
    }


    private void button1_Click(object sender, EventArgs e)
    {
        CommandRecognitionEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(CommandRecognized);

    }

    private static void CommandRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        MethodInfo myMethod;

        var methods = new Commands();//ERROR HERE: Commands is a namespace but used as a type

        myMethod = CommandFactory.GetCommandMethods(e.Result.Text);
        myMethod.Invoke(methods, null);
    }
}
}

您有一个 class 名为 Commands,但它也在命名空间 Commands 中,因此无法区分它们。

更改命名空间或 class 名称,或将该行更改为:

var methods = new Commands.Commands();

编译器无法区分您指的是 Class 还是命名空间 Commands,因为您有 class 和同名的命名空间。

执行以下任一操作

  • 重构你的class名字

  • 重构您的命名空间名称

  • 向编译器显示你的意思 class Commands.Commands