.NET アプリケーションの高速化について探し始めてみた - その3 - ( SuppressUnmanagedCodeSecurityAttribute属性 )
SuppressUnmanagedCodeSecurityAttribute() って知ってますか?ネットをぼーと探していたら見つけたのですが、ここにこの属性の説明が記載されています。
※下記はMSDNから抜粋
// SuppressSecurity.cs
using System;
using System.Security;
using System.Security.Permissions;
using System.Runtime.InteropServices;
class NativeMethods
{
// This is a call to unmanaged code. Executing this method requires
// the UnmanagedCode security permission. Without this permission,
// an attempt to call this method will throw a SecurityException:
/* NOTE: The SuppressUnmanagedCodeSecurityAttribute disables the
check for the UnmanagedCode permission at runtime. Be Careful! */
[SuppressUnmanagedCodeSecurityAttribute()]
[DllImport("msvcrt.dll")]
internal static extern int puts(string str);
[SuppressUnmanagedCodeSecurityAttribute()]
[DllImport("msvcrt.dll")]
internal static extern int _flushall();
}
アンマネージ コードのアクセス許可のチェックが、アンマネージ メソッドの呼び出し時に毎回行われるのではなく、読み込み時に 1 回だけ行われます。アンマネージ メソッドが繰り返し呼び出される場合は、パフォーマンスが大幅に向上します。
こんな属性あったんですね。全然知りませんでした。セキュリティを担保できているアプリケーションであれば、使用してみるのも良いかもしれません。