Encoding.RegisterProvider(EncodingProvider) Method

Definition

注册编码提供程序。Registers an encoding provider.

public:
 static void RegisterProvider(System::Text::EncodingProvider ^ provider);
[System.Security.SecurityCritical]
public static void RegisterProvider (System.Text.EncodingProvider provider);
public static void RegisterProvider (System.Text.EncodingProvider provider);
static member RegisterProvider : System.Text.EncodingProvider -> unit
Public Shared Sub RegisterProvider (provider As EncodingProvider)

Parameters

provider
EncodingProvider

提供对其他字符编码的访问权限的 EncodingProvider 的子类。A subclass of EncodingProvider that provides access to additional character encodings.

Attributes

Exceptions

providernullprovider is null.

Remarks

利用 RegisterProvider 方法,你可以注册派生自 EncodingProvider 的类,使字符编码在不支持它们的平台上可用。The RegisterProvider method allows you to register a class derived from EncodingProvider that makes character encodings available on a platform that does not otherwise support them. 注册编码提供程序后,可通过调用任何 Encoding.GetEncoding 重载来检索它支持的编码。Once the encoding provider is registered, the encodings that it supports can be retrieved by calling any Encoding.GetEncoding overload. 如果有多个编码提供程序,Encoding.GetEncoding 方法将尝试从每个提供程序检索指定的编码,并从最近注册的提供程序开始。If there are multiple encoding providers, the Encoding.GetEncoding method attempts to retrieve a specified encoding from each provider starting with the one most recently registered.

使用 RegisterProvider 方法注册编码提供程序还会在传递 0的参数时,修改encoding.getencoding (int32)encoding.getencoding (int32,EncoderFallback,DecoderFallback)方法的行为:Registering an encoding provider by using the RegisterProvider method also modifies the behavior of the Encoding.GetEncoding(Int32) and EncodingProvider.GetEncoding(Int32, EncoderFallback, DecoderFallback) methods when passed an argument of 0:

  • 如果注册的提供程序是 CodePagesEncodingProvider,则在 Windows 操作系统上运行时,该方法将返回与系统活动代码页匹配的编码。If the registered provider is the CodePagesEncodingProvider, the method returns the encoding that matches the system active code page when running on the Windows operating system.

  • 自定义编码提供程序可以选择在其中一个 GetEncoding 方法重载传递 0的参数时要返回哪种编码。A custom encoding provider can choose which encoding to return when either of these GetEncoding method overloads is passed an argument of 0. 提供程序还可以选择不通过让 EncodingProvider.GetEncoding 方法返回 null来返回编码。The provider can also choose to not return an encoding by having the EncodingProvider.GetEncoding method return null.

从 .NET Framework 4.6 开始,.NET Framework 包含一个 CodePagesEncodingProvider的编码提供程序,该提供程序使编码可用,该提供程序在完整 .NET Framework 中提供但在通用 Windows 平台中不可用。Starting with the .NET Framework 4.6, the .NET Framework includes one encoding provider, CodePagesEncodingProvider, that makes the encodings available that are present in the full .NET Framework but are not available in the Universal Windows Platform. 默认情况下,通用 Windows 平台仅支持 Unicode 编码、ASCII 和代码页28591。By default, the Universal Windows Platform only supports the Unicode encodings, ASCII, and code page 28591.

如果 RegisterProvider 方法的多个调用中使用了相同的编码提供程序,则只有第一个方法调用将注册该提供程序。If the same encoding provider is used in multiple calls to the RegisterProvider method, only the first method call registers the provider. 后续调用将被忽略。Subsequent calls are ignored.

如果调用 RegisterProvider 方法来注册处理相同编码的多个提供程序,则最后一个注册的提供程序将用于所有编码和解码操作。If the RegisterProvider method is called to register multiple providers that handle the same encoding, the last registered provider is the used for all encoding and decoding operations. 将忽略任何以前注册的提供程序。Any previously registered providers are ignored.

Applies to

See also