CharSet 枚举


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

public enum class CharSet
public enum CharSet
public enum CharSet
public enum CharSet
type CharSet = 
Public Enum CharSet


Ansi 2

将字符串封送为多字节字符串:对于 Windows 是系统默认的 Windows (ANSI) 代码页,对于 Unix 是 UTF-8。Marshal strings as multiple-byte character strings: the system default Windows (ANSI) code page on Windows, and UTF-8 on Unix.

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 1

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

Unicode 3

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


下面的代码示例演示了在将 StructLayoutAttribute 应用到托管定义时如何指定 CharSet 枚举值。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
    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. 此枚举提供字符集选项,由 DllImportAttributeStructLayoutAttribute使用。This 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.