風險降低:自訂 IMessageFilter.PreFilterMessage 實作Mitigation: Custom IMessageFilter.PreFilterMessage Implementations

在以 .NET Framework 4.6.1 和之後 .NET Framework 版本為目標的 Windows Forms 應用程式中,自訂的 IMessageFilter.PreFilterMessage 實作可以在呼叫 Application.FilterMessage 方法時安全地篩選訊息 (如果 IMessageFilter.PreFilterMessage 實作有下列狀況):In Windows Forms apps that target versions of the .NET Framework starting with the .NET Framework 4.6.1, a custom IMessageFilter.PreFilterMessage implementation can safely filter messages when the Application.FilterMessage method is called if the IMessageFilter.PreFilterMessage implementation:

影響Impact

這項變更只會影響以 .NET Framework 4.6.1 和更新版本為目標的 Windows Forms 應用程式。This change only affects Windows Forms apps that target versions of the .NET Framework starting with the .NET Framework 4.6.1.

針對以舊版 .NET Framework 為目標的 Windows Forms 應用程式,在某些情況下這類實作會在呼叫 Application.FilterMessage 方法時擲回 IndexOutOfRangeException 例外狀況For Windows Forms apps that target previous versions of the .NET Framework, such implementations in some cases throw an IndexOutOfRangeException exception when the Application.FilterMessage method is called

降低Mitigation

如果不需要這項變更,以 .NET Framework 4.6.1 或更新版本為目標的應用程式可以藉由在 <runtime> 應用程式佈建檔的區段中新增下列設定,來選擇不使用此項:If this change is undesirable, apps that target the .NET Framework 4.6.1 or a later version can opt out of it by adding the following configuration setting to the <runtime> section of the app’s configuration file:

<runtime>
    <AppContextSwitchOverrides value="Switch.System.Windows.Forms.DontSupportReentrantFilterMessage=true" />
</runtime>

此外,以舊版 .NET Framework 為目標但在 .NET Framework 4.6.1 或更新版本下執行的應用程式,可以藉由在 <runtime> 應用程式佈建檔的區段中新增下列設定,來加入宣告此行為:In addition, apps that target previous versions of the .NET Framework but are running under the .NET Framework 4.6.1 or a later version can opt in to this behavior by adding the following configuration setting to the <runtime> section of the app’s configuration file:

<runtime>
    <AppContextSwitchOverrides value="Switch.System.Windows.Forms.DontSupportReentrantFilterMessage=false" />
</runtime>

另請參閱See also