From 4a823330757e5ef728733f6cf1b4d5a60616710f Mon Sep 17 00:00:00 2001 From: Andrii Kurdiumov Date: Sun, 14 Nov 2021 13:18:55 +0600 Subject: [PATCH] Improve System.Speech trimmability by removing obvious warnings --- .../src/AudioFormat/AudioFormatConverter.cs | 4 +-- .../Internal/SapiInterop/SapiRecoInterop.cs | 6 ++-- .../src/Internal/SapiInterop/SpeechEvent.cs | 2 +- .../src/Internal/SrgsCompiler/BackEnd.cs | 16 +++++----- .../src/Internal/SrgsCompiler/CFGGrammar.cs | 20 ++++++------- .../src/Internal/StreamMarshaler.cs | 22 +++++++------- .../src/Internal/Synthesis/AudioBase.cs | 6 ++-- .../src/Internal/Synthesis/AudioDeviceOut.cs | 2 +- .../src/Internal/Synthesis/WaveHeader.cs | 2 +- .../src/Result/RecognitionResult.cs | 12 ++++---- .../src/Result/RecognizedPhrase.cs | 30 +++++++++---------- 11 files changed, 60 insertions(+), 62 deletions(-) diff --git a/src/libraries/System.Speech/src/AudioFormat/AudioFormatConverter.cs b/src/libraries/System.Speech/src/AudioFormat/AudioFormatConverter.cs index 66521fc6fc76ed..9bb69b74878208 100644 --- a/src/libraries/System.Speech/src/AudioFormat/AudioFormatConverter.cs +++ b/src/libraries/System.Speech/src/AudioFormat/AudioFormatConverter.cs @@ -14,10 +14,10 @@ internal static class AudioFormatConverter internal static SpeechAudioFormatInfo ToSpeechAudioFormatInfo(IntPtr waveFormatPtr) { - WaveFormatEx waveFormatEx = (WaveFormatEx)Marshal.PtrToStructure(waveFormatPtr, typeof(WaveFormatEx)); + WaveFormatEx waveFormatEx = Marshal.PtrToStructure(waveFormatPtr); byte[] extraData = new byte[waveFormatEx.cbSize]; - IntPtr extraDataPtr = new(waveFormatPtr.ToInt64() + Marshal.SizeOf(waveFormatEx)); + IntPtr extraDataPtr = new(waveFormatPtr.ToInt64() + Marshal.SizeOf()); for (int i = 0; i < waveFormatEx.cbSize; i++) { extraData[i] = Marshal.ReadByte(extraDataPtr, i); diff --git a/src/libraries/System.Speech/src/Internal/SapiInterop/SapiRecoInterop.cs b/src/libraries/System.Speech/src/Internal/SapiInterop/SapiRecoInterop.cs index 2c91bbd961b307..60999d287403ff 100644 --- a/src/libraries/System.Speech/src/Internal/SapiInterop/SapiRecoInterop.cs +++ b/src/libraries/System.Speech/src/Internal/SapiInterop/SapiRecoInterop.cs @@ -450,7 +450,7 @@ internal static ISpPhrase CreatePhraseFromWordUnits(RecognizedWordUnit[] words, SPPHRASEELEMENT[] elements = new SPPHRASEELEMENT[words.Length]; // build the unmanaged interop layer - int size = Marshal.SizeOf(typeof(SPPHRASEELEMENT)); + int size = Marshal.SizeOf(); List handles = new(); coMem = Marshal.AllocCoTaskMem(size * elements.Length); @@ -496,7 +496,7 @@ internal static ISpPhrase CreatePhraseFromWordUnits(RecognizedWordUnit[] words, elements[i].pszPronunciation = handle.AddrOfPinnedObject(); } - Marshal.StructureToPtr(elements[i], new IntPtr((long)coMem + size * i), false); + Marshal.StructureToPtr(elements[i], new IntPtr((long)coMem + size * i), false); } } finally @@ -505,7 +505,7 @@ internal static ISpPhrase CreatePhraseFromWordUnits(RecognizedWordUnit[] words, } SPPHRASE spPhrase = new(); - spPhrase.cbSize = (uint)Marshal.SizeOf(spPhrase.GetType()); + spPhrase.cbSize = (uint)Marshal.SizeOf(); spPhrase.LangID = (ushort)culture.LCID; spPhrase.Rule = new SPPHRASERULE { diff --git a/src/libraries/System.Speech/src/Internal/SapiInterop/SpeechEvent.cs b/src/libraries/System.Speech/src/Internal/SapiInterop/SpeechEvent.cs index 0758126ec01531..1616b7e105056a 100644 --- a/src/libraries/System.Speech/src/Internal/SapiInterop/SpeechEvent.cs +++ b/src/libraries/System.Speech/src/Internal/SapiInterop/SpeechEvent.cs @@ -26,7 +26,7 @@ private SpeechEvent(SPEVENTENUM eEventId, SPEVENTLPARAMTYPE elParamType, // Let the GC know if we have a unmanaged object with a given size if (_paramType == SPEVENTLPARAMTYPE.SPET_LPARAM_IS_POINTER || _paramType == SPEVENTLPARAMTYPE.SPET_LPARAM_IS_STRING) { - GC.AddMemoryPressure(_sizeMemoryPressure = Marshal.SizeOf(_lParam)); + GC.AddMemoryPressure(_sizeMemoryPressure = sizeof(ulong)); } } diff --git a/src/libraries/System.Speech/src/Internal/SrgsCompiler/BackEnd.cs b/src/libraries/System.Speech/src/Internal/SrgsCompiler/BackEnd.cs index b30d0c38cc6932..0f678c5c0f209c 100644 --- a/src/libraries/System.Speech/src/Internal/SrgsCompiler/BackEnd.cs +++ b/src/libraries/System.Speech/src/Internal/SrgsCompiler/BackEnd.cs @@ -1005,7 +1005,7 @@ private CfgGrammar.CfgSerializedHeader BuildHeader(List sortedStates, int } CfgGrammar.CfgSerializedHeader header = new(); - uint ulOffset = (uint)Marshal.SizeOf(typeof(CfgGrammar.CfgSerializedHeader)); + uint ulOffset = (uint)Marshal.SizeOf(); header.FormatId = CfgGrammar._SPGDF_ContextFree; _guid = Guid.NewGuid(); @@ -1032,16 +1032,16 @@ private CfgGrammar.CfgSerializedHeader BuildHeader(List sortedStates, int ulOffset += (uint)_symbols.SerializeSize() * Helpers._sizeOfChar; header.cRules = _rules.Count; header.pRules = ulOffset; - ulOffset += (uint)(_rules.Count * Marshal.SizeOf(typeof(CfgRule))); + ulOffset += (uint)(_rules.Count * Marshal.SizeOf()); header.cBasePath = cBasePath > 0 ? ulOffset : 0; //If there is no base path offset is set to zero ulOffset += (uint)((cBasePath * Helpers._sizeOfChar + 3) & ~3); header.cArcs = cArcs; header.pArcs = ulOffset; - ulOffset += (uint)(cArcs * Marshal.SizeOf(typeof(CfgArc))); + ulOffset += (uint)(cArcs * Marshal.SizeOf()); if (_fNeedWeightTable) { header.pWeights = ulOffset; - ulOffset += (uint)(cArcs * Marshal.SizeOf(typeof(float))); + ulOffset += (uint)(cArcs * sizeof(float)); pWeights = new float[cArcs]; pWeights[0] = 0.0f; } @@ -1067,16 +1067,16 @@ private CfgGrammar.CfgSerializedHeader BuildHeader(List sortedStates, int header.GrammarMode = (uint)_grammarMode; header.cTags = cSemanticTags; header.tags = ulOffset; - ulOffset += (uint)(cSemanticTags * Marshal.SizeOf(typeof(CfgSemanticTag))); + ulOffset += (uint)(cSemanticTags * Marshal.SizeOf()); header.cScripts = _scriptRefs.Count; header.pScripts = header.cScripts > 0 ? ulOffset : 0; - ulOffset += (uint)(_scriptRefs.Count * Marshal.SizeOf(typeof(CfgScriptRef))); + ulOffset += (uint)(_scriptRefs.Count * Marshal.SizeOf()); header.cIL = 0; header.pIL = 0; - ulOffset += (uint)(header.cIL * Marshal.SizeOf(typeof(byte))); + ulOffset += (uint)(header.cIL * sizeof(byte)); header.cPDB = 0; header.pPDB = 0; - ulOffset += (uint)(header.cPDB * Marshal.SizeOf(typeof(byte))); + ulOffset += (uint)(header.cPDB * sizeof(byte)); header.ulTotalSerializedSize = ulOffset; return header; } diff --git a/src/libraries/System.Speech/src/Internal/SrgsCompiler/CFGGrammar.cs b/src/libraries/System.Speech/src/Internal/SrgsCompiler/CFGGrammar.cs index 67bac9e6029aa9..410739a3be681f 100644 --- a/src/libraries/System.Speech/src/Internal/SrgsCompiler/CFGGrammar.cs +++ b/src/libraries/System.Speech/src/Internal/SrgsCompiler/CFGGrammar.cs @@ -272,7 +272,7 @@ internal static CfgHeader ConvertCfgHeader(StreamMarshaler streamHelper, bool in } //We know that in SAPI 5.0 grammar format pszWords follows header immediately. - if (cfgSerializedHeader.pszWords < Marshal.SizeOf(typeof(CfgSerializedHeader))) + if (cfgSerializedHeader.pszWords < Marshal.SizeOf()) { //This is SAPI 5.0 and SAPI 5.1 grammar format header.ulRootRuleIndex = 0xFFFFFFFF; @@ -294,7 +294,7 @@ internal static CfgHeader ConvertCfgHeader(StreamMarshaler streamHelper, bool in // Get the chars and build the string if (cfgSerializedHeader.cBasePath > 0) { - streamHelper.Stream.Position = (int)cfgSerializedHeader.pRules + (header.rules.Length * Marshal.SizeOf(typeof(CfgRule))); + streamHelper.Stream.Position = (int)cfgSerializedHeader.pRules + (header.rules.Length * Marshal.SizeOf()); header.BasePath = streamHelper.ReadNullTerminatedString(); } } @@ -319,7 +319,7 @@ internal static ScriptRef[] LoadScriptRefs(StreamMarshaler streamHelper, CfgSeri } //We know that in SAPI 5.0 grammar format pszWords follows header immediately. - if (pFH.pszWords < Marshal.SizeOf(typeof(CfgSerializedHeader))) + if (pFH.pszWords < Marshal.SizeOf()) { // Must be SAPI 6.0 or above to hold a .NET script return null; @@ -412,25 +412,25 @@ private static void CheckValidCfgFormat(CfgSerializedHeader pFH, CfgHeader heade //Check the rule offset if (pFH.cRules > 0) { - CheckSetOffsets(pFH.pRules, pFH.cRules * Marshal.SizeOf(typeof(CfgRule)), ref ullStartOffset, pFH.ulTotalSerializedSize); + CheckSetOffsets(pFH.pRules, pFH.cRules * Marshal.SizeOf(), ref ullStartOffset, pFH.ulTotalSerializedSize); } //Check the arc offset if (pFH.cArcs > 0) { - CheckSetOffsets(pFH.pArcs, pFH.cArcs * Marshal.SizeOf(typeof(CfgArc)), ref ullStartOffset, pFH.ulTotalSerializedSize); + CheckSetOffsets(pFH.pArcs, pFH.cArcs * Marshal.SizeOf(), ref ullStartOffset, pFH.ulTotalSerializedSize); } //Check the weight offset if (pFH.pWeights > 0) { - CheckSetOffsets(pFH.pWeights, pFH.cArcs * Marshal.SizeOf(typeof(float)), ref ullStartOffset, pFH.ulTotalSerializedSize); + CheckSetOffsets(pFH.pWeights, pFH.cArcs * sizeof(float), ref ullStartOffset, pFH.ulTotalSerializedSize); } //Check the semantic tag offset if (pFH.cTags > 0) { - CheckSetOffsets(pFH.tags, pFH.cTags * Marshal.SizeOf(typeof(CfgSemanticTag)), ref ullStartOffset, pFH.ulTotalSerializedSize); + CheckSetOffsets(pFH.tags, pFH.cTags * Marshal.SizeOf(), ref ullStartOffset, pFH.ulTotalSerializedSize); if (includeAllGrammarData) { @@ -465,17 +465,17 @@ private static void CheckValidCfgFormat(CfgSerializedHeader pFH, CfgHeader heade //Check the offset for the scripts if (pFH.cScripts > 0) { - CheckSetOffsets(pFH.pScripts, pFH.cScripts * Marshal.SizeOf(typeof(CfgScriptRef)), ref ullStartOffset, pFH.ulTotalSerializedSize); + CheckSetOffsets(pFH.pScripts, pFH.cScripts * Marshal.SizeOf(), ref ullStartOffset, pFH.ulTotalSerializedSize); } if (pFH.cIL > 0) { - CheckSetOffsets(pFH.pIL, pFH.cIL * Marshal.SizeOf(typeof(byte)), ref ullStartOffset, pFH.ulTotalSerializedSize); + CheckSetOffsets(pFH.pIL, pFH.cIL * sizeof(byte), ref ullStartOffset, pFH.ulTotalSerializedSize); } if (pFH.cPDB > 0) { - CheckSetOffsets(pFH.pPDB, pFH.cPDB * Marshal.SizeOf(typeof(byte)), ref ullStartOffset, pFH.ulTotalSerializedSize); + CheckSetOffsets(pFH.pPDB, pFH.cPDB * sizeof(byte), ref ullStartOffset, pFH.ulTotalSerializedSize); } } diff --git a/src/libraries/System.Speech/src/Internal/StreamMarshaler.cs b/src/libraries/System.Speech/src/Internal/StreamMarshaler.cs index 4742ff49787c4f..eba58d96cc8672 100644 --- a/src/libraries/System.Speech/src/Internal/StreamMarshaler.cs +++ b/src/libraries/System.Speech/src/Internal/StreamMarshaler.cs @@ -30,8 +30,7 @@ public void Dispose() #region internal Methods internal void ReadArray(T[] ao, int c) { - Type type = typeof(T); - int sizeOfOne = Marshal.SizeOf(type); + int sizeOfOne = Marshal.SizeOf(); int sizeObject = sizeOfOne * c; byte[] ab = Helpers.ReadStreamToByteArray(_stream, sizeObject); @@ -40,21 +39,20 @@ internal void ReadArray(T[] ao, int c) Marshal.Copy(ab, 0, buffer, sizeObject); for (int i = 0; i < c; i++) { - ao[i] = (T)Marshal.PtrToStructure((IntPtr)((long)buffer + i * sizeOfOne), type); + ao[i] = Marshal.PtrToStructure((IntPtr)((long)buffer + i * sizeOfOne)); } } internal void WriteArray(T[] ao, int c) { - Type type = typeof(T); - int sizeOfOne = Marshal.SizeOf(type); + int sizeOfOne = Marshal.SizeOf(); int sizeObject = sizeOfOne * c; byte[] ab = new byte[sizeObject]; IntPtr buffer = _safeHMem.Buffer(sizeObject); for (int i = 0; i < c; i++) { - Marshal.StructureToPtr(ao[i], (IntPtr)((long)buffer + i * sizeOfOne), false); + Marshal.StructureToPtr(ao[i], (IntPtr)((long)buffer + i * sizeOfOne), false); } Marshal.Copy(buffer, ab, 0, sizeObject); @@ -116,24 +114,24 @@ internal void WriteArrayChar(char[] ach, int c) } } - internal void ReadStream(object o) + internal void ReadStream(T o) { - int sizeObject = Marshal.SizeOf(o.GetType()); + int sizeObject = Marshal.SizeOf(); byte[] ab = Helpers.ReadStreamToByteArray(_stream, sizeObject); IntPtr buffer = _safeHMem.Buffer(sizeObject); Marshal.Copy(ab, 0, buffer, sizeObject); - Marshal.PtrToStructure(buffer, o); + Marshal.PtrToStructure(buffer, o); } - internal void WriteStream(object o) + internal void WriteStream(T o) { - int sizeObject = Marshal.SizeOf(o.GetType()); + int sizeObject = Marshal.SizeOf(); byte[] ab = new byte[sizeObject]; IntPtr buffer = _safeHMem.Buffer(sizeObject); - Marshal.StructureToPtr(o, buffer, false); + Marshal.StructureToPtr(o, buffer, false); Marshal.Copy(buffer, ab, 0, sizeObject); // Read the Header diff --git a/src/libraries/System.Speech/src/Internal/Synthesis/AudioBase.cs b/src/libraries/System.Speech/src/Internal/Synthesis/AudioBase.cs index b5650685fbeacb..ff57d09872075e 100644 --- a/src/libraries/System.Speech/src/Internal/Synthesis/AudioBase.cs +++ b/src/libraries/System.Speech/src/Internal/Synthesis/AudioBase.cs @@ -234,10 +234,10 @@ internal static void WriteWaveHeader(Stream stream, WAVEFORMATEX waveEx, long po BLOCKHDR block = new(0); DATAHDR dataHdr = new(0); - int cRiff = Marshal.SizeOf(riff); - int cBlock = Marshal.SizeOf(block); + int cRiff = Marshal.SizeOf(); + int cBlock = Marshal.SizeOf(); int cWaveEx = waveEx.Length;// Marshal.SizeOf (waveEx); // The CLR automatically pad the waveEx structure to dword boundary. Force 16. - int cDataHdr = Marshal.SizeOf(dataHdr); + int cDataHdr = Marshal.SizeOf(); int total = cRiff + cBlock + cWaveEx + cDataHdr; diff --git a/src/libraries/System.Speech/src/Internal/Synthesis/AudioDeviceOut.cs b/src/libraries/System.Speech/src/Internal/Synthesis/AudioDeviceOut.cs index 9df323e94c7dac..a40b1e855a014e 100644 --- a/src/libraries/System.Speech/src/Internal/Synthesis/AudioDeviceOut.cs +++ b/src/libraries/System.Speech/src/Internal/Synthesis/AudioDeviceOut.cs @@ -313,7 +313,7 @@ internal static MMSYSERR GetDeviceName(int deviceId, [MarshalAs(UnmanagedType.LP prodName = string.Empty; SafeNativeMethods.WAVEOUTCAPS caps = new(); - MMSYSERR result = SafeNativeMethods.waveOutGetDevCaps((IntPtr)deviceId, ref caps, Marshal.SizeOf(caps)); + MMSYSERR result = SafeNativeMethods.waveOutGetDevCaps((IntPtr)deviceId, ref caps, Marshal.SizeOf()); if (result != MMSYSERR.NOERROR) { return result; diff --git a/src/libraries/System.Speech/src/Internal/Synthesis/WaveHeader.cs b/src/libraries/System.Speech/src/Internal/Synthesis/WaveHeader.cs index ea569ba86ff350..b90879db18a6f9 100644 --- a/src/libraries/System.Speech/src/Internal/Synthesis/WaveHeader.cs +++ b/src/libraries/System.Speech/src/Internal/Synthesis/WaveHeader.cs @@ -86,7 +86,7 @@ internal int SizeHDR { get { - return Marshal.SizeOf(_waveHdr); + return Marshal.SizeOf(); } } diff --git a/src/libraries/System.Speech/src/Result/RecognitionResult.cs b/src/libraries/System.Speech/src/Result/RecognitionResult.cs index 2df46ff8555d36..b3fca40bce68ea 100644 --- a/src/libraries/System.Speech/src/Result/RecognitionResult.cs +++ b/src/libraries/System.Speech/src/Result/RecognitionResult.cs @@ -151,7 +151,7 @@ public RecognizedAudio Audio { IntPtr audioBuffer = gc.AddrOfPinnedObject(); - SPWAVEFORMATEX audioHeader = (SPWAVEFORMATEX)Marshal.PtrToStructure(audioBuffer, typeof(SPWAVEFORMATEX)); + SPWAVEFORMATEX audioHeader = Marshal.PtrToStructure(audioBuffer); IntPtr rawDataBuffer = new((long)audioBuffer + audioHeader.cbUsed); byte[] rawAudioData = new byte[audioLength - audioHeader.cbUsed]; @@ -296,15 +296,15 @@ private void Initialize(IRecognizerInternal recognizer, ISpRecoResult recoResult int headerSize = Marshal.ReadInt32(buffer, 4); // Read header size directly from buffer - 4 is the offset of cbHeaderSize. - if (headerSize == Marshal.SizeOf(typeof(SPRESULTHEADER_Sapi51))) // SAPI 5.1 size + if (headerSize == Marshal.SizeOf()) // SAPI 5.1 size { - SPRESULTHEADER_Sapi51 legacyHeader = (SPRESULTHEADER_Sapi51)Marshal.PtrToStructure(buffer, typeof(SPRESULTHEADER_Sapi51)); + SPRESULTHEADER_Sapi51 legacyHeader = Marshal.PtrToStructure(buffer); _header = new SPRESULTHEADER(legacyHeader); _isSapi53Header = false; } else { - _header = (SPRESULTHEADER)Marshal.PtrToStructure(buffer, typeof(SPRESULTHEADER)); + _header = Marshal.PtrToStructure(buffer); _isSapi53Header = true; } @@ -355,12 +355,12 @@ private Collection ExtractAlternates(int numberOfAlternates, b { IntPtr buffer = gc.AddrOfPinnedObject(); - int sizeOfSpSerializedPhraseAlt = Marshal.SizeOf(typeof(SPSERIALIZEDPHRASEALT)); + int sizeOfSpSerializedPhraseAlt = Marshal.SizeOf(); int offset = 0; for (int i = 0; i < numberOfAlternates; i++) { IntPtr altBuffer = new((long)buffer + offset); - SPSERIALIZEDPHRASEALT alt = (SPSERIALIZEDPHRASEALT)Marshal.PtrToStructure(altBuffer, typeof(SPSERIALIZEDPHRASEALT)); + SPSERIALIZEDPHRASEALT alt = Marshal.PtrToStructure(altBuffer); offset += sizeOfSpSerializedPhraseAlt; // advance over SPSERIALIZEDPHRASEALT if (isSapi53Header) diff --git a/src/libraries/System.Speech/src/Result/RecognizedPhrase.cs b/src/libraries/System.Speech/src/Result/RecognizedPhrase.cs index ebcde98e50aaf9..ecd91138f297bf 100644 --- a/src/libraries/System.Speech/src/Result/RecognizedPhrase.cs +++ b/src/libraries/System.Speech/src/Result/RecognizedPhrase.cs @@ -159,7 +159,7 @@ public ReadOnlyCollection Words int elementsOffset = (int)_serializedPhrase.ElementsOffset; List wordList = new(countOfElements); - int sizeofPhraseElement = Marshal.SizeOf(typeof(SPSERIALIZEDPHRASEELEMENT)); + int sizeofPhraseElement = Marshal.SizeOf(); GCHandle gc = GCHandle.Alloc(_phraseBuffer, GCHandleType.Pinned); try @@ -168,7 +168,7 @@ public ReadOnlyCollection Words for (int i = 0; i < countOfElements; i++) { IntPtr elementBuffer = new((long)buffer + elementsOffset + i * sizeofPhraseElement); - SPSERIALIZEDPHRASEELEMENT element = (SPSERIALIZEDPHRASEELEMENT)Marshal.PtrToStructure(elementBuffer, typeof(SPSERIALIZEDPHRASEELEMENT)); + SPSERIALIZEDPHRASEELEMENT element = Marshal.PtrToStructure(elementBuffer); string displayForm = null, lexicalForm = null, pronunciation = null; if (element.pszDisplayTextOffset != 0) @@ -292,9 +292,9 @@ public Collection ReplacementWordUnits // Get the ITN and Look for replacement phrase/ IntPtr itnBuffer = new((long)buffer + _serializedPhrase.ReplacementsOffset); - for (int i = 0; i < _serializedPhrase.cReplacements; i++, itnBuffer = (IntPtr)((long)itnBuffer + Marshal.SizeOf(typeof(SPPHRASEREPLACEMENT)))) + for (int i = 0; i < _serializedPhrase.cReplacements; i++, itnBuffer = (IntPtr)((long)itnBuffer + Marshal.SizeOf())) { - SPPHRASEREPLACEMENT replacement = (SPPHRASEREPLACEMENT)Marshal.PtrToStructure(itnBuffer, typeof(SPPHRASEREPLACEMENT)); + SPPHRASEREPLACEMENT replacement = (SPPHRASEREPLACEMENT)Marshal.PtrToStructure(itnBuffer); string text = Marshal.PtrToStringUni(new IntPtr((long)buffer + replacement.pszReplacementText)); DisplayAttributes displayAttributes = RecognizedWordUnit.SapiAttributesToDisplayAttributes(replacement.bDisplayAttributes); _replacementText.Add(new ReplacementText(displayAttributes, text, (int)replacement.ulFirstElement, (int)replacement.ulCountOfElements)); @@ -325,11 +325,11 @@ internal static SPSERIALIZEDPHRASE GetPhraseHeader(IntPtr phraseBuffer, uint exp if (isSapi53Header) { - serializedPhrase = (SPSERIALIZEDPHRASE)Marshal.PtrToStructure(phraseBuffer, typeof(SPSERIALIZEDPHRASE)); + serializedPhrase = Marshal.PtrToStructure(phraseBuffer); } else { - SPSERIALIZEDPHRASE_Sapi51 legacyPhrase = (SPSERIALIZEDPHRASE_Sapi51)Marshal.PtrToStructure(phraseBuffer, typeof(SPSERIALIZEDPHRASE_Sapi51)); + SPSERIALIZEDPHRASE_Sapi51 legacyPhrase = Marshal.PtrToStructure(phraseBuffer); serializedPhrase = new SPSERIALIZEDPHRASE(legacyPhrase); } @@ -539,7 +539,7 @@ private static SemanticValue RecursiveBuildSemanticProperties(IList propertyList, int semanticsOffset, IntPtr phraseBuffer, RuleNode ruleTree, IList words, bool isSapi53Header) { IntPtr propertyBuffer = new((long)phraseBuffer + semanticsOffset); - SPSERIALIZEDPHRASEPROPERTY property = (SPSERIALIZEDPHRASEPROPERTY)Marshal.PtrToStructure(propertyBuffer, typeof(SPSERIALIZEDPHRASEPROPERTY)); + SPSERIALIZEDPHRASEPROPERTY property = Marshal.PtrToStructure(propertyBuffer); string propertyName; SemanticValue thisSemanticValue = ExtractSemanticValueInformation(semanticsOffset, property, phraseBuffer, isSapi53Header, out propertyName); @@ -572,7 +572,7 @@ private void RecursivelyExtractSemanticValue(IntPtr phraseBuffer, int semanticsO { IntPtr propertyBuffer = new((long)phraseBuffer + semanticsOffset); SPSERIALIZEDPHRASEPROPERTY property = - (SPSERIALIZEDPHRASEPROPERTY)Marshal.PtrToStructure(propertyBuffer, typeof(SPSERIALIZEDPHRASEPROPERTY)); + Marshal.PtrToStructure(propertyBuffer); string propertyName; SemanticValue thisSemanticValue = ExtractSemanticValueInformation(semanticsOffset, property, phraseBuffer, isSapi53Header, out propertyName); @@ -695,7 +695,7 @@ private static SemanticValue ExtractSemanticValueInformation(int semanticsOffset break; case VarEnum.VT_UI4: - propertyValue = Marshal.PtrToStructure(valueBuffer, typeof(uint)); + propertyValue = Marshal.PtrToStructure(valueBuffer); break; case VarEnum.VT_I8: @@ -703,15 +703,15 @@ private static SemanticValue ExtractSemanticValueInformation(int semanticsOffset break; case VarEnum.VT_UI8: - propertyValue = Marshal.PtrToStructure(valueBuffer, typeof(ulong)); + propertyValue = Marshal.PtrToStructure(valueBuffer); break; case VarEnum.VT_R4: - propertyValue = Marshal.PtrToStructure(valueBuffer, typeof(float)); + propertyValue = Marshal.PtrToStructure(valueBuffer); break; case VarEnum.VT_R8: - propertyValue = Marshal.PtrToStructure(valueBuffer, typeof(double)); + propertyValue = Marshal.PtrToStructure(valueBuffer); break; case VarEnum.VT_BOOL: @@ -754,7 +754,7 @@ private static RuleNode ExtractRules(Grammar grammar, SPSERIALIZEDPHRASERULE rul if (rule.NextSiblingOffset > 0) { IntPtr elementBuffer = new((long)phraseBuffer + rule.NextSiblingOffset); - SPSERIALIZEDPHRASERULE ruleNext = (SPSERIALIZEDPHRASERULE)Marshal.PtrToStructure(elementBuffer, typeof(SPSERIALIZEDPHRASERULE)); + SPSERIALIZEDPHRASERULE ruleNext = Marshal.PtrToStructure(elementBuffer); node._next = ExtractRules(grammar, ruleNext, phraseBuffer); } @@ -762,7 +762,7 @@ private static RuleNode ExtractRules(Grammar grammar, SPSERIALIZEDPHRASERULE rul if (rule.FirstChildOffset > 0) { IntPtr elementBuffer = new((long)phraseBuffer + rule.FirstChildOffset); - SPSERIALIZEDPHRASERULE ruleFirst = (SPSERIALIZEDPHRASERULE)Marshal.PtrToStructure(elementBuffer, typeof(SPSERIALIZEDPHRASERULE)); + SPSERIALIZEDPHRASERULE ruleFirst = Marshal.PtrToStructure(elementBuffer); node._child = ExtractRules(grammar, ruleFirst, phraseBuffer); } @@ -781,7 +781,7 @@ private void ThrowInvalidSemanticInterpretationError() { IntPtr smlBuffer = gc.AddrOfPinnedObject(); - SPSEMANTICERRORINFO semanticError = (SPSEMANTICERRORINFO)Marshal.PtrToStructure((IntPtr)((long)smlBuffer + (int)_serializedPhrase.SemanticErrorInfoOffset), typeof(SPSEMANTICERRORINFO)); + SPSEMANTICERRORINFO semanticError = Marshal.PtrToStructure((IntPtr)((long)smlBuffer + (int)_serializedPhrase.SemanticErrorInfoOffset)); string source = Marshal.PtrToStringUni(new IntPtr((long)smlBuffer + semanticError.pszSourceOffset)); string description = Marshal.PtrToStringUni(new IntPtr((long)smlBuffer + semanticError.pszDescriptionOffset));