BestFitMappingAttribute BestFitMappingAttribute BestFitMappingAttribute BestFitMappingAttribute Class

定义

控制是否将 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.Struct | System.AttributeTargets.Interface, Inherited=false)]
[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
继承
BestFitMappingAttributeBestFitMappingAttributeBestFitMappingAttributeBestFitMappingAttribute
属性

示例

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

默认情况下,公共语言运行时将转换为 ANSI 字符任何托管 Unicode 字符传递给非托管的方法正在执行 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. 例如,封送处理程序将转换到 c 的非托管方法接受 ANSI 字符的 Unicode 版权字符。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. 同样,可以启用或禁用最佳匹配映射平台 invoke 调用通过使用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,无法映射的字符生成了异常时ThrowOnUnmappableChar字段是trueEven 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.

注意

不能更改由提供的默认值BestFitMappingAttribute时将托管的数组传递其元素是 ANSI 字符或 Lpstr 到非托管的安全数组。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(Boolean) BestFitMappingAttribute(Boolean) BestFitMappingAttribute(Boolean)

初始化 BestFitMappingAttribute 类的新实例,并将其设置为 BestFitMapping 属性的值。Initializes a new instance of the BestFitMappingAttribute class set to the value of the BestFitMapping property.

字段

ThrowOnUnmappableChar ThrowOnUnmappableChar ThrowOnUnmappableChar ThrowOnUnmappableChar

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

属性

BestFitMapping BestFitMapping BestFitMapping BestFitMapping

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

TypeId TypeId TypeId TypeId

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

(Inherited from Attribute)

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Attribute)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Attribute)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute()

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

(Inherited from Attribute)
Match(Object) Match(Object) Match(Object) Match(Object)

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

(Inherited from Attribute)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
ToString() ToString() ToString() ToString()

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

(Inherited from Object)

显式界面实现

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

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

适用于

另请参阅