Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / Speech / Src / Internal / SrgsCompiler / AppDomainCompilerProxy.cs / 1 / AppDomainCompilerProxy.cs
//// Copyright (c) Microsoft Corporation. All rights reserved. // // // // Description: // AppDomainProxy for the SrgsCompiler // // History: // 10/1/2004 jeanfp Created //---------------------------------------------------------------------------- #region Using directives using System; using System.Collections.Generic; using System.Globalization; using System.Reflection; using System.Speech.Internal.SrgsParser; using System.Speech.Recognition; using System.Speech.Recognition.SrgsGrammar; using System.Text; #endregion #pragma warning disable 1634, 1691 // Allows suppression of certain PreSharp messages. #pragma warning disable 56500 // Remove all the catch all statements warnings used by the interop layer // This class is used to validate the content of a strongly typed grammar. It is loaded in an app domain. namespace System.Speech.Internal.SrgsCompiler { ////// TODOC /// internal class AppDomainCompilerProxy : MarshalByRefObject { // This method is used. It is referenced through reflection internal Exception CheckAssembly (byte [] il, int iCfg, string language, string nameSpace, string [] ruleNames, string [] methodNames, int [] methodScripts) { try { Assembly assembly = Assembly.Load (il); // Allocate the array of string for all the constructors _constructors = new string [ruleNames.Length]; // Validate the rule scripts definition for (int i = 0, count = ruleNames.Length; i < count; i++) { string sRule = ruleNames [i]; string sMethod = methodNames [i]; _constructors [i] = string.Empty; // Get the class defition string classname = (!string.IsNullOrEmpty (nameSpace) ? nameSpace + "." : string.Empty) + sRule; Type typeClass = assembly.GetType (classname); if (typeClass == null) { XmlParser.ThrowSrgsException (SRID.CannotFindClass, sRule, nameSpace); } // Make sure that it derives from Grammar if (!(typeClass.IsSubclassOf (typeof (System.Speech.Recognition.Grammar)))) { XmlParser.ThrowSrgsException (SRID.StrongTypedGrammarNotAGrammar, classname, nameSpace); } // Get all the method names to check the parameters MethodInfo [] methods = typeClass.GetMethods (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); ScriptRefStruct ruleScript = new ScriptRefStruct (sRule, (RuleMethodScript) methodScripts [i]); bool found = false; for (int iMethod = 0; iMethod < methods.Length; iMethod++) { MethodInfo method = methods [iMethod]; if (method.Name == sMethod) { // Check for the parameters ParameterInfo [] args = method.GetParameters (); Type returnType = null; switch (ruleScript._method) { case RuleMethodScript.onInit: // Add the parameters for the new overload _constructors [i] += GenerateConstructor (iCfg, args, language, sRule); // build the returned type returnType = typeof (SrgsRule []); break; case RuleMethodScript.onParse: ThrowIfMultipleOverloads (found, sMethod); if (args.Length != 2 || args [0].ParameterType != typeof (SemanticValue) || args [1].ParameterType != typeof (RecognizedWordUnit [])) { XmlParser.ThrowSrgsException (SRID.RuleScriptInvalidParameters, sMethod, ruleScript._rule); } returnType = typeof (object); break; case RuleMethodScript.onRecognition: ThrowIfMultipleOverloads (found, sMethod); if (args.Length != 1 || args [0].ParameterType != typeof (RecognitionResult)) { XmlParser.ThrowSrgsException (SRID.RuleScriptInvalidParameters, sMethod, ruleScript._rule); } returnType = typeof (object); break; case RuleMethodScript.onError: ThrowIfMultipleOverloads (found, sMethod); if (args.Length != 1 || args [0].ParameterType != typeof (Exception)) { XmlParser.ThrowSrgsException (SRID.RuleScriptInvalidParameters, sMethod, ruleScript._rule); } returnType = typeof (void); break; } // Check for the return type if (method.ReturnType != returnType) { XmlParser.ThrowSrgsException (SRID.RuleScriptInvalidReturnType, sMethod, ruleScript._rule); } found = true; } } if (!found) { XmlParser.ThrowSrgsException (SRID.RuleScriptNotFound, sMethod, ruleScript._rule, ruleScript._method.ToString ()); } // The class needs to be public if (!typeClass.IsPublic) { XmlParser.ThrowSrgsException (SRID.ClassNotPublic, sRule); } } } catch (Exception e) { return e; } return null; } internal string [] Constructors () { return _constructors; } internal string GenerateConstructor (int iCfg, ParameterInfo [] parameters, string language, string classname) { string script = string.Empty; // Select an instance of a compiler and wrap the script code with the // class definition switch (language) { case "C#": script = WrapConstructorCSharp (iCfg, parameters, classname); break; case "VB.Net": script = WrapConstructorVB (iCfg, parameters, classname); break; default: XmlParser.ThrowSrgsException (SRID.UnsupportedLanguage, language); break; } return script; } static private void ThrowIfMultipleOverloads (bool found, string method) { if (found) { XmlParser.ThrowSrgsException (SRID.OverloadNotAllowed, method); } } static private string WrapConstructorCSharp (int iCfg, ParameterInfo [] parameters, string classname) { StringBuilder sb = new StringBuilder (200); sb.Append (" public "); sb.Append (classname); sb.Append (" ("); if (parameters != null) { int i = 0; foreach (ParameterInfo arg in parameters) { if (i++ > 0) { sb.Append (", "); } if (i == parameters.Length && arg.ParameterType.IsArray) { object [] customAttributes = arg.GetCustomAttributes (false); foreach (object attribute in customAttributes) { if (attribute is ParamArrayAttribute) { sb.Append ("params "); break; } } } sb.Append (arg.ParameterType.FullName); sb.Append (" "); sb.Append (arg.Name); } } sb.Append (" )\n {\n object [] onInitParams = new object ["); sb.Append (parameters == null ? 0 : parameters.Length); sb.Append ("];\n"); for (int iArg = 0; parameters != null && iArg < parameters.Length; iArg++) { sb.Append ("onInitParams ["); sb.Append (iArg); sb.Append ("] = "); sb.Append (parameters [iArg].Name); sb.Append (";\n"); } sb.Append ("ResourceName = \""); sb.Append (iCfg.ToString (CultureInfo.InvariantCulture)); sb.Append (".CFG\";\nStgInit (onInitParams);"); sb.Append ("\n } \n"); return sb.ToString (); } static private string WrapConstructorVB (int iCfg, ParameterInfo [] parameters, string classname) { StringBuilder sb = new StringBuilder (200); sb.Append ("Public Sub New"); sb.Append (" ("); if (parameters != null) { int i = 0; foreach (ParameterInfo arg in parameters) { if (i++ > 0) { sb.Append (", "); } if (!arg.ParameterType.IsByRef) { sb.Append ("ByVal "); } if (i == parameters.Length && arg.ParameterType.IsArray) { object [] customAttributes = arg.GetCustomAttributes (false); foreach (object attribute in customAttributes) { if (attribute is ParamArrayAttribute) { sb.Append ("ParamArray "); break; } } } sb.Append (arg.Name); if (arg.ParameterType.IsArray) { sb.Append ("()"); } sb.Append (" as "); sb.Append (arg.ParameterType.Name); } } sb.Append (" )\n Dim onInitParams () as Object = {"); for (int iArg = 0; parameters != null && iArg < parameters.Length; iArg++) { if (iArg > 0) { sb.Append (", "); } sb.Append (parameters [iArg].Name); } sb.Append ("}\n"); sb.Append ("ResourceName = \""); sb.Append (iCfg.ToString (CultureInfo.InvariantCulture)); sb.Append (".CFG\"\nStgInit (onInitParams)\n"); sb.Append ("\nEnd Sub \n"); return sb.ToString (); } internal string [] _constructors; //******************************************************************* // // Private Types // //******************************************************************* #region Private Types ////// Summary description for ScriptRef. /// // list of rules with scripts private class ScriptRefStruct { //******************************************************************* // // Constructors // //******************************************************************** #region Constructors internal ScriptRefStruct (string rule, RuleMethodScript method) { _rule = rule; _method = method; } #endregion //******************************************************************* // // Internal Fields // //******************************************************************** #region Internal Fields internal string _rule; internal RuleMethodScript _method; #endregion } #endregion } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //// Copyright (c) Microsoft Corporation. All rights reserved. // // // // Description: // AppDomainProxy for the SrgsCompiler // // History: // 10/1/2004 jeanfp Created //---------------------------------------------------------------------------- #region Using directives using System; using System.Collections.Generic; using System.Globalization; using System.Reflection; using System.Speech.Internal.SrgsParser; using System.Speech.Recognition; using System.Speech.Recognition.SrgsGrammar; using System.Text; #endregion #pragma warning disable 1634, 1691 // Allows suppression of certain PreSharp messages. #pragma warning disable 56500 // Remove all the catch all statements warnings used by the interop layer // This class is used to validate the content of a strongly typed grammar. It is loaded in an app domain. namespace System.Speech.Internal.SrgsCompiler { ////// TODOC /// internal class AppDomainCompilerProxy : MarshalByRefObject { // This method is used. It is referenced through reflection internal Exception CheckAssembly (byte [] il, int iCfg, string language, string nameSpace, string [] ruleNames, string [] methodNames, int [] methodScripts) { try { Assembly assembly = Assembly.Load (il); // Allocate the array of string for all the constructors _constructors = new string [ruleNames.Length]; // Validate the rule scripts definition for (int i = 0, count = ruleNames.Length; i < count; i++) { string sRule = ruleNames [i]; string sMethod = methodNames [i]; _constructors [i] = string.Empty; // Get the class defition string classname = (!string.IsNullOrEmpty (nameSpace) ? nameSpace + "." : string.Empty) + sRule; Type typeClass = assembly.GetType (classname); if (typeClass == null) { XmlParser.ThrowSrgsException (SRID.CannotFindClass, sRule, nameSpace); } // Make sure that it derives from Grammar if (!(typeClass.IsSubclassOf (typeof (System.Speech.Recognition.Grammar)))) { XmlParser.ThrowSrgsException (SRID.StrongTypedGrammarNotAGrammar, classname, nameSpace); } // Get all the method names to check the parameters MethodInfo [] methods = typeClass.GetMethods (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); ScriptRefStruct ruleScript = new ScriptRefStruct (sRule, (RuleMethodScript) methodScripts [i]); bool found = false; for (int iMethod = 0; iMethod < methods.Length; iMethod++) { MethodInfo method = methods [iMethod]; if (method.Name == sMethod) { // Check for the parameters ParameterInfo [] args = method.GetParameters (); Type returnType = null; switch (ruleScript._method) { case RuleMethodScript.onInit: // Add the parameters for the new overload _constructors [i] += GenerateConstructor (iCfg, args, language, sRule); // build the returned type returnType = typeof (SrgsRule []); break; case RuleMethodScript.onParse: ThrowIfMultipleOverloads (found, sMethod); if (args.Length != 2 || args [0].ParameterType != typeof (SemanticValue) || args [1].ParameterType != typeof (RecognizedWordUnit [])) { XmlParser.ThrowSrgsException (SRID.RuleScriptInvalidParameters, sMethod, ruleScript._rule); } returnType = typeof (object); break; case RuleMethodScript.onRecognition: ThrowIfMultipleOverloads (found, sMethod); if (args.Length != 1 || args [0].ParameterType != typeof (RecognitionResult)) { XmlParser.ThrowSrgsException (SRID.RuleScriptInvalidParameters, sMethod, ruleScript._rule); } returnType = typeof (object); break; case RuleMethodScript.onError: ThrowIfMultipleOverloads (found, sMethod); if (args.Length != 1 || args [0].ParameterType != typeof (Exception)) { XmlParser.ThrowSrgsException (SRID.RuleScriptInvalidParameters, sMethod, ruleScript._rule); } returnType = typeof (void); break; } // Check for the return type if (method.ReturnType != returnType) { XmlParser.ThrowSrgsException (SRID.RuleScriptInvalidReturnType, sMethod, ruleScript._rule); } found = true; } } if (!found) { XmlParser.ThrowSrgsException (SRID.RuleScriptNotFound, sMethod, ruleScript._rule, ruleScript._method.ToString ()); } // The class needs to be public if (!typeClass.IsPublic) { XmlParser.ThrowSrgsException (SRID.ClassNotPublic, sRule); } } } catch (Exception e) { return e; } return null; } internal string [] Constructors () { return _constructors; } internal string GenerateConstructor (int iCfg, ParameterInfo [] parameters, string language, string classname) { string script = string.Empty; // Select an instance of a compiler and wrap the script code with the // class definition switch (language) { case "C#": script = WrapConstructorCSharp (iCfg, parameters, classname); break; case "VB.Net": script = WrapConstructorVB (iCfg, parameters, classname); break; default: XmlParser.ThrowSrgsException (SRID.UnsupportedLanguage, language); break; } return script; } static private void ThrowIfMultipleOverloads (bool found, string method) { if (found) { XmlParser.ThrowSrgsException (SRID.OverloadNotAllowed, method); } } static private string WrapConstructorCSharp (int iCfg, ParameterInfo [] parameters, string classname) { StringBuilder sb = new StringBuilder (200); sb.Append (" public "); sb.Append (classname); sb.Append (" ("); if (parameters != null) { int i = 0; foreach (ParameterInfo arg in parameters) { if (i++ > 0) { sb.Append (", "); } if (i == parameters.Length && arg.ParameterType.IsArray) { object [] customAttributes = arg.GetCustomAttributes (false); foreach (object attribute in customAttributes) { if (attribute is ParamArrayAttribute) { sb.Append ("params "); break; } } } sb.Append (arg.ParameterType.FullName); sb.Append (" "); sb.Append (arg.Name); } } sb.Append (" )\n {\n object [] onInitParams = new object ["); sb.Append (parameters == null ? 0 : parameters.Length); sb.Append ("];\n"); for (int iArg = 0; parameters != null && iArg < parameters.Length; iArg++) { sb.Append ("onInitParams ["); sb.Append (iArg); sb.Append ("] = "); sb.Append (parameters [iArg].Name); sb.Append (";\n"); } sb.Append ("ResourceName = \""); sb.Append (iCfg.ToString (CultureInfo.InvariantCulture)); sb.Append (".CFG\";\nStgInit (onInitParams);"); sb.Append ("\n } \n"); return sb.ToString (); } static private string WrapConstructorVB (int iCfg, ParameterInfo [] parameters, string classname) { StringBuilder sb = new StringBuilder (200); sb.Append ("Public Sub New"); sb.Append (" ("); if (parameters != null) { int i = 0; foreach (ParameterInfo arg in parameters) { if (i++ > 0) { sb.Append (", "); } if (!arg.ParameterType.IsByRef) { sb.Append ("ByVal "); } if (i == parameters.Length && arg.ParameterType.IsArray) { object [] customAttributes = arg.GetCustomAttributes (false); foreach (object attribute in customAttributes) { if (attribute is ParamArrayAttribute) { sb.Append ("ParamArray "); break; } } } sb.Append (arg.Name); if (arg.ParameterType.IsArray) { sb.Append ("()"); } sb.Append (" as "); sb.Append (arg.ParameterType.Name); } } sb.Append (" )\n Dim onInitParams () as Object = {"); for (int iArg = 0; parameters != null && iArg < parameters.Length; iArg++) { if (iArg > 0) { sb.Append (", "); } sb.Append (parameters [iArg].Name); } sb.Append ("}\n"); sb.Append ("ResourceName = \""); sb.Append (iCfg.ToString (CultureInfo.InvariantCulture)); sb.Append (".CFG\"\nStgInit (onInitParams)\n"); sb.Append ("\nEnd Sub \n"); return sb.ToString (); } internal string [] _constructors; //******************************************************************* // // Private Types // //******************************************************************* #region Private Types ////// Summary description for ScriptRef. /// // list of rules with scripts private class ScriptRefStruct { //******************************************************************* // // Constructors // //******************************************************************** #region Constructors internal ScriptRefStruct (string rule, RuleMethodScript method) { _rule = rule; _method = method; } #endregion //******************************************************************* // // Internal Fields // //******************************************************************** #region Internal Fields internal string _rule; internal RuleMethodScript _method; #endregion } #endregion } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- RequestCache.cs
- DataTable.cs
- SqlProfileProvider.cs
- WpfGeneratedKnownTypes.cs
- PeerResolverBindingElement.cs
- CorePropertiesFilter.cs
- CacheManager.cs
- BCLDebug.cs
- ObjectParameter.cs
- Exceptions.cs
- QueryContinueDragEvent.cs
- CheckBox.cs
- ValueQuery.cs
- ExtendedPropertyCollection.cs
- TrackingParameters.cs
- ProviderConnectionPointCollection.cs
- DefaultAutoFieldGenerator.cs
- QualificationDataAttribute.cs
- ConfigurationSettings.cs
- XomlCompilerHelpers.cs
- TdsRecordBufferSetter.cs
- Separator.cs
- ScriptManagerProxy.cs
- TypefaceMetricsCache.cs
- EmptyWorkItem.cs
- Repeater.cs
- ProfessionalColorTable.cs
- SplayTreeNode.cs
- TypedDatasetGenerator.cs
- MorphHelpers.cs
- DataGridViewLayoutData.cs
- XmlRootAttribute.cs
- ComponentResourceKey.cs
- TypeValidationEventArgs.cs
- Stack.cs
- PermissionToken.cs
- SafePEFileHandle.cs
- WSHttpBindingCollectionElement.cs
- Rotation3D.cs
- LeaseManager.cs
- CodeDomSerializationProvider.cs
- Vector3DCollectionValueSerializer.cs
- Triplet.cs
- WindowsGrip.cs
- FixedSOMSemanticBox.cs
- FilteredReadOnlyMetadataCollection.cs
- TransformerInfoCollection.cs
- TypeExtension.cs
- HyperLinkField.cs
- IDictionary.cs
- XmlSchemaObjectTable.cs
- PerfCounters.cs
- LineBreakRecord.cs
- ObjectDisposedException.cs
- SBCSCodePageEncoding.cs
- SamlAction.cs
- TextRangeEditTables.cs
- ItemList.cs
- MouseActionValueSerializer.cs
- ForwardPositionQuery.cs
- XslTransform.cs
- XmlToDatasetMap.cs
- DataSourceHelper.cs
- ZoneIdentityPermission.cs
- PageAsyncTaskManager.cs
- DbModificationCommandTree.cs
- Int32.cs
- HttpListener.cs
- CodeIdentifiers.cs
- ContentDisposition.cs
- SubqueryRules.cs
- SoapHeaderException.cs
- SmiSettersStream.cs
- SqlVisitor.cs
- AdCreatedEventArgs.cs
- ServerType.cs
- ThicknessConverter.cs
- UniqueConstraint.cs
- ToolStripSeparator.cs
- CodeExpressionStatement.cs
- _TransmitFileOverlappedAsyncResult.cs
- TablePattern.cs
- WindowsGraphics.cs
- DefinitionBase.cs
- PageCodeDomTreeGenerator.cs
- EncodingNLS.cs
- DataTemplateKey.cs
- ListViewItemSelectionChangedEvent.cs
- UnsafeNativeMethodsMilCoreApi.cs
- RichTextBoxConstants.cs
- BinaryObjectInfo.cs
- Keywords.cs
- Tokenizer.cs
- DbCommandDefinition.cs
- SQLDoubleStorage.cs
- WithStatement.cs
- HandlerWithFactory.cs
- AutomationElementCollection.cs
- SemaphoreSlim.cs
- SqlRecordBuffer.cs