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. 雖然 Common Language Runtime 預設為 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 套用至 managed 定義時,指定 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


因為有數個非受控字串類型,而且只有一個 managed 字串類型,所以您必須使用字元集來指定如何將 managed 字串封送處理為非受控碼。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.