BestFitMappingAttribute 類別

定義

控制 Unicode 字元是否要轉換成最相近的 ANSI 字元。Controls whether Unicode characters are converted to the closest matching ANSI characters.

public ref class BestFitMappingAttribute sealed : Attribute
[System.Runtime.InteropServices.ComVisible(true)]
[System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Struct, Inherited=false)]
public sealed class BestFitMappingAttribute : Attribute
type BestFitMappingAttribute = class
    inherit Attribute
Public NotInheritable Class BestFitMappingAttribute
Inherits Attribute
繼承
BestFitMappingAttribute
屬性

範例

下列範例示範如何停用最適合的對應,並在 Unicode 字元轉換為 '? ' 時擲回例外狀況。ANSI 字元。The following example shows how to disable best fit mapping and to throw an exception on the conversion of Unicode characters to the '?' ANSI character. 以這種方式設定 BestFitMappingAttribute 參數,可提供額外的安全性量值。Setting BestFitMappingAttribute parameters in this manner provides an added measure of security.

[BestFitMapping(false, ThrowOnUnmappableChar = true)]
interface class IMyInterface1
{
     //Insert code here.
};
[BestFitMapping(false, ThrowOnUnmappableChar = true)]
interface IMyInterface1
{
     //Insert code here.
}
<BestFitMapping(False, ThrowOnUnmappableChar := True)> _
Interface IMyInterface1
     'Insert code here.
End Interface

備註

您可以將此屬性套用至元件、介面、類別或結構。You can apply this attribute to an assembly, interface, class, or structure.

根據預設,通用語言執行平臺會將任何傳遞至在 Windows 98 或 Windows Me 上執行之非受控方法的 managed Unicode 字元轉換成 ANSI 字元。By default, the common language runtime converts to ANSI characters any managed Unicode characters passed to an unmanaged method executing on Windows 98 or Windows Me. 自動調整對應可讓 interop 封送處理器在沒有完全相符的存在時,選取關閉比對的字元。Best-fit mapping enables the interop marshaler to select a close-matching character when no exact match exists. 例如,封送處理器會針對接受 ANSI 字元的非受控方法,將 Unicode 著作權字元轉換成 ' c '。For example, the marshaler converts the Unicode copyright character to 'c' for unmanaged methods that accept ANSI characters.

警告

某些字元缺少最適合的標記法;這些字元稱為無法映射。Some characters lack a best-fit representation; these characters are called unmappable. 無法映射的字元通常會轉換成預設的 '? 'ANSI 字元。Unmappable characters are usually converted to the default '?' ANSI character. 某些 Unicode 字元會轉換成危險的字元(例如反斜線 '\' 字元),這可能會不小心變更路徑。Certain Unicode characters are converted to dangerous characters, such as the backslash '\' character, which can inadvertently change a path.

BestFitMappingAttribute 提供兩個參數來控制最適合的對應層面。BestFitMappingAttribute provides two parameters to control aspects of best-fit mapping. 您可以使用第一個參數來開啟和關閉自動調整對應。You use the first parameter to toggle best-fit mapping on and off. 預設值為 true,這可讓您在元件、介面和類別層級上進行最符合的對應。The default value is true, which enables best-fit mapping on the assembly, interface, and class levels. 套用至介面或類別的屬性會覆寫元件層級屬性。An attribute applied to an interface or class overrides an assembly-level attribute. 同樣地,您可以使用 [DllImportAttribute.BestFitMapping] 欄位,啟用或停用平台叫用呼叫的自動調整對應。You can likewise enable or disable best-fit mapping for platform invoke calls by using the DllImportAttribute.BestFitMapping field. 「平台叫用」欄位所設定的值會覆寫所有層級的 BestFitMappingAttributeA value set by the platform invoke field overrides all levels of BestFitMappingAttribute.

您可以使用第二個參數來控制在無法映射的字元上擲回例外狀況。You can use the second parameter to control throwing an exception on unmappable characters. [ThrowOnUnmappableChar] 欄位的預設值為 [false],這會在每次執行時間遇到必須轉換成 '? ' 的 Unicode 字元時,停用擲回例外狀況。ANSI 字元。The default value for the ThrowOnUnmappableChar field is false, which disables throwing an exception each time the runtime encounters a Unicode character that has to be converted to the '?' ANSI character. 即使 true自動調整對應,當 trueThrowOnUnmappableChar 欄位時,無法對應的字元也會產生例外狀況。Even if best-fit mapping is true, unmappable characters generate an exception when the ThrowOnUnmappableChar field is true. 若要加強安全性,您可以將第一個參數切換為 false,第二個參數則設為 trueTo tighten security, you can toggle the first parameter to false and the second parameter to true. 這組參數設定會關閉自動調整對應,但會將例外狀況擲回機制視為安全預防措施。This combination of parameter settings turns best-fit mapping off, but enables the exception-throwing mechanism as a safety precaution.

警告

將 managed 陣列(其元素為 ANSI 字元或 LPSTRs)傳遞至不受管理的安全陣列時,您無法變更 BestFitMappingAttribute 所提供的預設值。You cannot change the default values provided by BestFitMappingAttribute when passing a managed array whose elements are ANSI Chars or LPSTRs to an unmanaged safe array. 自動調整對應一律為啟用狀態,而且不會擲回任何例外狀況。Best-fit mapping is always enabled and no exception can be thrown. 請注意,這種組合可能會危害您的安全性模型。Be aware that this combination can compromise your security model.

建構函式

BestFitMappingAttribute(Boolean)

初始化 BestFitMappingAttribute 類別的新執行個體,這個執行個體設定為 BestFitMapping 屬性的值。Initializes a new instance of the BestFitMappingAttribute class set to the value of the BestFitMapping property.

欄位

ThrowOnUnmappableChar

在無法對應的 Unicode 字元轉換為 ANSI '?' 字元時,啟用或停用擲回例外狀況。Enables or disables the throwing of an exception on an unmappable Unicode character that is converted to an ANSI '?' character.

屬性

BestFitMapping

將 Unicode 字元轉換為 ANSI 字元時,取得最適合對應行為。Gets the best-fit mapping behavior when converting Unicode characters to ANSI characters.

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。When implemented in a derived class, gets a unique identifier for this Attribute.

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。Returns a value that indicates whether this instance is equal to a specified object.

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

(繼承來源 Attribute)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。Maps a set of names to a corresponding set of dispatch identifiers.

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。Retrieves the type information for an object, which can be used to get the type information for an interface.

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。Provides access to properties and methods exposed by an object.

(繼承來源 Attribute)

適用於

另請參閱