public static MethodInfo GetSpeechMethod(string speechText) { MethodInfo methodInfo; var mySpeechMethods = new MySpeechMethods(); if (speechMethods.Count == 0) { var methodNames = typeof(MySpeechMethods).GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance); var speechAttributeMethods = methodNames.Where(y => y.GetCustomAttributes().OfType <SpeechAttribute>().Any()); foreach (var speechAttributeMethod in speechAttributeMethods) { foreach (var attribute in speechAttributeMethod.GetCustomAttributes(true)) { speechMethods.Add(((SpeechAttribute)attribute).SpeechValue, speechAttributeMethod); } } methodInfo = speechMethods[speechText]; } else { methodInfo = speechMethods[speechText]; } return(methodInfo); }
static void Main(string[] args) { var methods = new MySpeechMethods(); MethodInfo myMethod; myMethod = SpeechFactory.GetSpeechMethod("Time Please"); myMethod.Invoke(methods, null); myMethod = SpeechFactory.GetSpeechMethod("Volume Down"); myMethod.Invoke(methods, null); myMethod = SpeechFactory.GetSpeechMethod("Volume Up"); myMethod.Invoke(methods, null); }
static void Main(string[] args) { var methods = new MySpeechMethods(); MethodInfo myMethod; myMethod = SpeechFactory.GetSpeechMethod("Thank you"); myMethod.Invoke(methods, null); myMethod = SpeechFactory.GetSpeechMethod("Say something funny"); myMethod.Invoke(methods, null); myMethod = SpeechFactory.GetSpeechMethod("I said funny dammit!"); myMethod.Invoke(methods, null); }