// Default constructor voor President: standaard worden alle speeches persoonlijk geschreeuwd en op Twitter gedumpt
 public President()
 {
     // Netjes een nieuwe delegate-instance voor de methode DeliverSpeechPersonally aanmaken, en aan PerformSpeech toewijzen.
     // (NB: door gebruik van '=' worden eventuele eerdere delegates in PerformSpeech *vervangen* door deze nieuwe delegate
     PerformSpeech = new SpeechMethod(SpeakPersonally);
     // De korte versie (C# regelt zelf instantiatie van een delegate), en toe*voegen* (+=) aan PerformSpeech.
     PerformSpeech += DumpOnTwitter;
 }
        /// <summary>
        /// creates a new command
        /// </summary>
        /// <param name="method">the method</param>
        /// <param name="invokeInstance">the instance to invoke the method on (or null for static methods)</param>
        public void AddMethod(SpeechMethod method, object invokeInstance)
        {
            Dictionary <string, SpeechGroupTuple> langCommands;

            //create lang if not exists
            if (Commands.TryGetValue(method.Lang, out langCommands) == false)
            {
                langCommands = new Dictionary <string, SpeechGroupTuple>();
                Commands.Add(method.Lang, langCommands);
            }


            //add the method to the right speech group

            SpeechGroupTuple speechGroup;

            if (langCommands.TryGetValue(method.SpeechGroupKey, out speechGroup) == false)
            {
                speechGroup = new SpeechGroupTuple(true);
                langCommands.Add(method.SpeechGroupKey, speechGroup);
            }

            //check all synonyms

            if (method.SpeechNames.Any(p => speechGroup.Commands.ContainsKey(p)))
            {
                //there is a synonyme that is already another speech name in this speech group -> invalid
                throw new Exception("speech dictionary already contains a method with speech name: " + method.SpeechNames + " and key: " + method.Key);
            }
            else
            {
                var cmd = new SpeechTuple(true, method, invokeInstance);
                foreach (var speechName in method.SpeechNames)
                {
                    speechGroup.Commands.Add(speechName, cmd); //same command with different names...
                }
            }
        }
示例#3
0
 /// <summary>
 /// creates a new SpeechTuple
 /// </summary>
 /// <param name="isEnabled">true: group is enabled, false: not</param>
 /// <param name="method">the method</param>
 /// <param name="invokeInstance">the invoking instance</param>
 public SpeechTuple(bool isEnabled, SpeechMethod method, object invokeInstance)
 {
     this.IsEnabled      = isEnabled;
     this.Method         = method;
     this.InvokeInstance = invokeInstance;
 }
示例#4
0
        /// <summary>
        /// crawls a method
        /// </summary>
        /// <param name="methodInfo">the method info</param>
        /// <param name="type">the class of the method</param>
        /// <param name="converters">the list of converters</param>
        /// <returns>[1. lang, 2. speech method]</returns>
        private static Dictionary <string, SpeechMethod> CrawlMethod(MethodInfo methodInfo, Type type, Dictionary <string, SpeechParameterConverter> converters)
        {
            var mattributes = methodInfo.GetCustomAttributes(typeof(SpeechMethodAttribute), false);

            var crawledMethods = new Dictionary <string, SpeechMethod>();

            if (mattributes.Length > 0)
            {
                IEnumerable <SpeechMethodAttribute> speechMethodAttributes = mattributes.Cast <SpeechMethodAttribute>();

                foreach (var speechMethodAttribute in speechMethodAttributes)
                {
                    //set method parameters
                    var speechMethod = new SpeechMethod();
                    speechMethod.Key            = speechMethodAttribute.Key;
                    speechMethod.SpeechGroupKey = speechMethodAttribute.SpeechGroupKey;
                    speechMethod.Lang           = speechMethodAttribute.Lang;
                    speechMethod.SpeechNames.AddRange(speechMethodAttribute.SpeechNames);
                    speechMethod.MethodInfo    = methodInfo;
                    speechMethod.ExecutingType = type;

                    crawledMethods.Add(speechMethod.Lang, speechMethod);
                }

                //now get possible parameters
                var parameters = methodInfo.GetParameters();
                foreach (var parameterInfo in parameters)
                {
                    var pattributes = parameterInfo.GetCustomAttributes(typeof(SpeechParameterAttribute), false);

                    if (pattributes.Length > 0)
                    {
                        var parameterAttributes = pattributes.Cast <SpeechParameterAttribute>();

                        foreach (var publicSpeechArgumentAttribute in parameterAttributes)
                        {
                            //set parameter parameters
                            var speechParameter = new SpeechParameter();
                            speechParameter.SpeechNames   = publicSpeechArgumentAttribute.SpeechNames;
                            speechParameter.ParameterInfo = parameterInfo;


                            //check if we have a converter for this parameter
                            SpeechParameterConverter converter;
                            if (converters.TryGetValue(publicSpeechArgumentAttribute.ConverterKey, out converter))
                            {
                                //converter return type and parameter type must be equal
                                if (converter.MethodInfo.ReturnType == parameterInfo.ParameterType)
                                {
                                    speechParameter.Converter = converter;
                                }
                                else
                                {
                                    throw new Exception("the type returned by the converter: " + converter.Key + " (" + converter.MethodInfo.ReturnType + ") is not equal to the " +
                                                        "parameter type: " + parameterInfo.ParameterType + "(type: " + type.FullName + " method:" +
                                                        methodInfo.Name + " parameter " + parameterInfo.Name + ")");
                                }
                            }
                            else
                            {
                                throw new Exception("cannot find parameter converter with key: " + publicSpeechArgumentAttribute.ConverterKey + "\ntype: " +
                                                    type.FullName + " method: " + methodInfo.Name + " parameter: " + parameterInfo.Name);
                            }

                            SpeechMethod speechMethod;

                            if (crawledMethods.TryGetValue(publicSpeechArgumentAttribute.Lang, out speechMethod))
                            {
                                if (speechMethod.Arguments.Any(p => p.ParameterInfo.Name == parameterInfo.Name))
                                {
                                    throw new Exception("only one parameter attribute per language is allowed, language: " + publicSpeechArgumentAttribute.Lang
                                                        + " parameter: " + parameterInfo.Name);
                                }
                                else
                                {
                                    speechMethod.Arguments.Add(speechParameter);
                                }
                            }
                            else
                            {
                                throw new Exception("no " + typeof(SpeechMethodAttribute).FullName + " specified for parameter: " +
                                                    parameterInfo.Name + " for lang: " + publicSpeechArgumentAttribute.Lang + " on type: " + type.FullName);
                            }
                        }
                    }
                }
            }

            return(crawledMethods);
        }