public SpeechParameterInfo(SpeechParameter parameter) { this.Parameter = parameter; }
/// <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); }