BestFitMappingAttribute 类


控制是否将 Unicode 字符转换为最接近的匹配 ANSI 字符。Controls whether Unicode characters are converted to the closest matching ANSI characters.

public ref class BestFitMappingAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Struct, Inherited=false)]
public sealed class BestFitMappingAttribute : Attribute
[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


下面的示例演示如何禁用最佳匹配映射,并在将 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.

默认情况下,公共语言运行时将任何托管的 Unicode 字符转换为 ANSI 字符,这些字符传递给在 Windows 98 或 Windows Me 上执行的非托管方法。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. 最佳映射使互操作封送拆收器可以在不存在精确匹配时选择匹配的字符。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. 平台调用字段设置的值将覆盖 BestFitMappingAttribute的所有级别。A 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最适合的映射,当 ThrowOnUnmappableChar 字段 true时,无法映射的字符也会生成异常。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.


在将元素为 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 类的新实例,并将其设置为 BestFitMapping 属性的值。Initializes a new instance of the BestFitMappingAttribute class set to the value of the BestFitMapping property.



启用或禁用在遇到已被转换为 ANSI“?”字符的无法映射的 Unicode 字符时引发异常。Enables or disables the throwing of an exception on an unmappable Unicode character that is converted to an ANSI '?' character.



获取将 Unicode 字符转换为 ANSI 字符时的最佳映射行为。Gets the best-fit mapping behavior when converting Unicode characters to ANSI characters.


在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(继承自 Attribute)



返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(继承自 Attribute)

返回此实例的哈希代码。Returns the hash code for this instance.

(继承自 Attribute)

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(继承自 Attribute)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(继承自 Attribute)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)


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

将一组名称映射为对应的一组调度标识符。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)

检索对象提供的类型信息接口的数量(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)