BestFitMappingAttribute 类

定义

控制是否将 Unicode 字符转换为最接近的匹配 ANSI 字符。

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)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class BestFitMappingAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Struct, Inherited=false)>]
type BestFitMappingAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Struct, Inherited=false)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type BestFitMappingAttribute = class
    inherit Attribute
Public NotInheritable Class BestFitMappingAttribute
Inherits Attribute
继承
BestFitMappingAttribute
属性

示例

以下示例演示如何禁用最适合的映射,以及如何在 Unicode 字符转换为“?”时引发异常ANSI 字符。 以这种方式设置 BestFitMappingAttribute 参数可提供额外的安全措施。

[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

注解

可以将此属性应用于程序集、接口、类或结构。

注意

某些字符缺少最适合的表示形式;这些字符称为“不可应用”。 不可应用的字符通常转换为默认的“?”ANSI 字符。 某些 Unicode 字符会转换为危险字符,例如反斜杠“\”字符,这可能会无意中更改路径。

BestFitMappingAttribute 提供两个参数来控制最适合映射的各个方面。 使用第一个参数来打开和关闭最适合的映射。 默认值为 true,它支持在程序集、接口和类级别上实现最佳拟合映射。 应用于接口或类的属性会替代程序集级属性。 同样,可以使用 字段为平台调用 DllImportAttribute.BestFitMapping 启用或禁用最适合的映射。 平台调用字段设置的值会替代 的所有级别 BestFitMappingAttribute

可以使用第二个参数来控制对不可应用字符引发异常。 字段的 ThrowOnUnmappableChar 默认值为 false,它禁止在运行时每次遇到必须转换为“?”的 Unicode 字符时引发异常。ANSI 字符。 即使最适合的映射为 ,当 字段为 truetrueThrowOnUnmappableChar,不可应用字符也会生成异常。 若要加强安全性,可以将第一个参数 false 切换到 ,将第二个参数切换到 true。 参数设置的这种组合会关闭最适合的映射,但启用异常引发机制作为安全预防措施。

注意

在将元素为 ANSI Chars 或 LPSTR 的托管数组传递到非托管安全数组时,不能更改提供的 BestFitMappingAttribute 默认值。 最佳匹配映射始终处于启用状态,并且不会引发异常。 请注意,此组合可能会危及安全模型。

构造函数

BestFitMappingAttribute(Boolean)

初始化 BestFitMappingAttribute 类的新实例,并将其设置为 BestFitMapping 属性的值。

字段

ThrowOnUnmappableChar

启用或禁用在遇到已被转换为 ANSI“?”字符的无法映射的 Unicode 字符时引发异常。

属性

BestFitMapping

获取将 Unicode 字符转换为 ANSI 字符时的最佳映射行为。

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。

(继承自 Attribute)

方法

Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。

(继承自 Attribute)
GetHashCode()

返回此实例的哈希代码。

(继承自 Attribute)
GetType()

获取当前实例的 Type

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。

(继承自 Attribute)
Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。

(继承自 Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

显式接口实现

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

将一组名称映射为对应的一组调度标识符。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。

(继承自 Attribute)

适用于

另请参阅