CharSet CharSet CharSet CharSet Enum

定义

决定封送字符串应使用的字符集。Dictates which character set marshaled strings should use.

public enum class CharSet
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public enum CharSet
type CharSet = 
Public Enum CharSet
继承
属性

字段

Ansi Ansi Ansi Ansi 2

以多字节字符串的形式封送字符串。Marshal strings as multiple-byte character strings.

Auto Auto Auto Auto 4

针对目标操作系统适当地自动封送字符串。Automatically marshal strings appropriately for the target operating system. 请参阅字符集和封送处理有关详细信息。See Charsets and marshaling for details. 尽管公共语言运行时默认值为 Auto,但语言可替代此默认值。Although the common language runtime default is Auto, languages may override this default. 例如,默认情况下,C# 和 Visual Basic 将所有方法和类型都标记为 AnsiFor example, by default C# and Visual Basic mark all methods and types as Ansi.

None None None None 1

此值已过时,它与 Ansi 具有相同的行为。This value is obsolete and has the same behavior as Ansi.

Unicode Unicode Unicode Unicode 3

字符串封送为 Unicode 2 字节字符的字符串。Marshal strings as Unicode 2-byte character strings.

示例

下面的代码示例演示如何指定CharSet枚举值应用时StructLayoutAttribute到托管定义。The following code example shows how to specify a CharSet enumeration value when applying the StructLayoutAttribute to a managed definition.

[StructLayout(LayoutKind::Sequential, CharSet=CharSet::Ansi)]
public ref struct MyPerson
{
public:
    String^ first;
    String^ last;
};
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct MyPerson
{
    public String first;
    public String last;
}
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure MyPerson
    Public first As String
    Public last As String
End Structure

注解

因为有多个非托管的字符串类型,并且仅一种托管字符串类型,你必须使用字符集来指定如何将托管的字符串应封送到非托管代码。Because there are several unmanaged string types and only one managed string type, you must use a character set to specify how managed strings should be marshaled to unmanaged code. 此枚举提供了字符设置选项,可供DllImportAttributeStructLayoutAttributeThis enumeration, which provides character set options, is used by DllImportAttribute and StructLayoutAttribute. 字符串封送和名称匹配行为与此枚举相关联的详细说明,请参阅指定字符集For a detailed description of the string marshaling and name matching behavior associated with this enumeration, see Specifying a Character Set.

适用于

另请参阅