title: Smart Way to Handle Voice Commands in Code to Action a Command
body: <p>Rather than using Switch/Case or IF Boolean checks that can get very long and awfully tedious, I wonder if a better way can be sought for handling and processing Commands.</p>
  
  <p>E.G:</p>
  
  <pre><code>if(settings.getName == Command)
  {
  Speak("I am here");
  }
  
  if("Get News Feed" == Command)
  {
  MyRSSFeed RSSNewsFeed = new MyRSSFeed();
  RSSNewsFeed.GetFeed();
  }
  </code></pre>
  
  <p>The if commands go on... Here is a snippet of my Switch Statement:</p>
  
  <pre><code>switch (Command)
          {
              #region &lt;-- Get Time Command --&gt;
  
              case "Time Please":
              case "Whats the Time":
              case "What Time is it":
                  GetCurrentTime();
                  break;
  
              #endregion &lt;-- Get Time Command --&gt;
  
              #region &lt;-- Get Date Command --&gt;
  
              case "Whats the Date":
              case "What Date is it":
              case "Whats the Date Today":
              case "What is the Date Today":
                  GetCurrentDate();
                  break;
  
              #endregion &lt;-- Get Date Command --&gt;
  
  
              #region &lt;-- Media Player Commands --&gt;
  
              case "Play Bamboo Forest":
  
                  Data.MusicPlayer.Play(@"\Bamboo Forest Play List.wpl");
  
                  break;
  
              case "Next Song":
  
                  Data.MusicPlayer.Next();
  
                  break;
  
              case "Previous Song":
  
                  Data.MusicPlayer.Previous();
  
                  break;
  
              case "Stop Music":
  
                  Data.MusicPlayer.Stop();
  
                  break;
  
              case "Pause Music":
  
                  Data.MusicPlayer.Pause();
  
                  break;
  
              case "Resume Music":
  
                  Data.MusicPlayer.Resume();
  
                  break;
  
  
              case "Mute Music":
  
                  Data.MusicPlayer.Mute();
  
                  break;
  
              case "Volume Up":
  
                  Data.MusicPlayer.VolumeUp();
  
                  break;
  
              case "Volume Down":
  
                  Data.MusicPlayer.VolumeDown();
  
                  break;
  
              #endregion &lt;-- Media Player Commands --&gt;
  
              #region &lt;-- Voice Recognition Control Commands --&gt;
  
              case "Stop Listening":
                  Audio.Listen.NewCommandRecognitionEngine.RecognizeAsyncCancel();
                  Audio.Voice.Speak("Ok");
                  Audio.Listen.Initialise(main);
                  break;
  
              #endregion &lt;-- Voice Recognition Control Commands --&gt;
  
              #region &lt;-- Application Commands --&gt;
  
              case "Quiet":
                  Audio.Voice.Stop();
                  break;
  
              case "Download":
                  Audio.Voice.Speak("Opening Download Window.");
                  main.dlInterface.ShowBitsJobs();
                  break;
  
              case "Settings":
                  Audio.Voice.Speak("Opening Settings Window.");
                  main.settings.Show();
                  break;
  
              case "Close":
                  if (main.dlInterface.Visable == true)
                  {
                      main.dlInterface.Hide();
                      Audio.Voice.Speak("Closing Download Window.");
                  }
                  if (main.settings.Visible == true)
                  {
                      main.settings.Hide();
                      Audio.Voice.Speak("Closing Settings Window.");
                  }
                  break;
  
              case "Out of the way":
                  if (main.WindowState == System.Windows.Forms.FormWindowState.Normal)
                  {
                      main.WindowState = System.Windows.Forms.FormWindowState.Minimized;
                      Audio.Voice.Speak("My apologies");
                  }
                  break;
  
              case "Where Are You":
                  if (main.WindowState == System.Windows.Forms.FormWindowState.Minimized)
                  {
                      main.WindowState = System.Windows.Forms.FormWindowState.Normal;
                      Audio.Voice.Speak("Here");
                  }
                  break;
  
  
              default:
                  // Do Nothing here...
                  break;
          }
  </code></pre>
  
  <p>I have a SQL Database that contains Commands. I Load Commands into it as I need. It has a Command Name Colum and a Value Colum. I can change these to add change or delete columns as needed.</p>
  
  <p>Currently, once a command is recognised, I use a combination of IF Statements and a Switch/Case Catch to catch the recognised Command.</p>
  
  <p>I have thought about somehow dropping dll's into a folder and some how scanning then on app load. If I add a Command then somehow use the Value Field to action the command in the dll.</p>
  
  <p>I realise this is a rather complex situation but I feel a much better solution can be found to make this process much more simple.</p>
  
  <p>EDIT: I have looked at this already: <a href="http://social.msdn.microsoft.com/Forums/en-US/4f962dc0-aec2-4191-9fe2-e1dfeb1da5dd/voice-command-api" rel="nofollow">http://social.msdn.microsoft.com/Forums/en-US/4f962dc0-aec2-4191-9fe2-e1dfeb1da5dd/voice-command-api</a></p>
  
  <p>Please ask if you need any more information. </p>
  
  <p><strong>[EDIT]</strong> Paqogomez has answered this question. See my working example below:</p>
  
  <pre><code>using System;
  using System.Linq;
  using MyApp.AppCommands;
  using System.Reflection;
  using System.Collections.Generic;
  
  namespace MyApp
  {
  class Program
  {
  static void Main(string[] args)
  {
  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);
  
  Console.ReadLine();
  }
  }
  
  public static class CommandFactory
  {
  private static Dictionary&lt;string, MethodInfo&gt; commandMethods = new Dictionary&lt;string, MethodInfo&gt;();
  
  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 =&gt; y.GetCustomAttributes().OfType&lt;CommandAttribute&gt;().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;
  }
  }
  }
  
  namespace MyApp.AppCommands
  {
  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;
  }
  }
  }
  </code></pre>
  
  <p>Beautiful Work Paqogomez and thank you for Sharing! This is fast and very elegant!.</p>
  
  <p>In my case, all I need to call the code is:</p>
  
  <pre><code>private static void CommandRecognized(object sender, SpeechRecognizedEventArgs e)
  {
  MethodInfo myMethod;
  
  var methods = new Commands();
  
  myMethod = CommandFactory.GetCommandMethods(e.Result.Text);
  myMethod.Invoke(methods, null);
  }
  </code></pre>
  
  <p>which is the Event Handler of the Speech Recognition Engine:</p>
  
  <pre><code>CommandRecognitionEngine.SpeechRecognized += new EventHandler&lt;SpeechRecognizedEventArgs&gt;(CommandRecognized);
  </code></pre>