Bewerken

Share via


EncoderFallback Class

Definition

Provides a failure-handling mechanism, called a fallback, for an input character that cannot be converted to an encoded output byte sequence.

public ref class EncoderFallback abstract
public abstract class EncoderFallback
[System.Serializable]
public abstract class EncoderFallback
type EncoderFallback = class
[<System.Serializable>]
type EncoderFallback = class
Public MustInherit Class EncoderFallback
Inheritance
EncoderFallback
Derived
Attributes

Remarks

An encoding maps a Unicode character to an encoded sequence of bytes. A particular encoding is represented by a type that is derived from the Encoding class. Specifically, a character is encoded to a byte sequence by calling the encoding type's Encoding.GetBytes method, and the byte sequence is decoded to a character array or a string by calling the Encoding.GetChars or Encoding.GetString method.

An encoding operation can fail if the input character cannot be represented by the encoding. For example, an ASCIIEncoding object cannot encode a character whose Unicode code point value is outside the range U+0000 to U+007F.

When an encoding conversion cannot be performed, the .NET Framework provides a failure-handling mechanism called a fallback. Your application can use predefined .NET Framework encoder fallbacks, or it can create a custom encoder fallback derived from the EncoderFallback and EncoderFallbackBuffer classes.

EncoderFallback and EncoderFallbackBuffer are the base classes for all encoding fallback handlers in the .NET Framework. They support the following three kinds of fallback handling mechanisms:

  • Best-fit fallback, which maps valid Unicode characters that cannot be encoded to an approximate equivalent. For example, a best-fit fallback handler for the ASCIIEncoding class might map Æ (U+00C6) to AE (U+0041 + U+0045). A best-fit fallback handler might also be implemented to transliterate one alphabet (such as Cyrillic) to another (such as Latin or Roman). The .NET Framework does not provide any public best-fit fallback implementations.

  • Replacement fallback, which replaces each character that cannot be encoded with a predefined string. The .NET Framework provides a predefined replacement fallback handler. The EncoderReplacementFallback class replaces each byte sequence that cannot be decoded with a question mark character ("?", or U+003F) or a REPLACEMENT CHARACTER (U+FFFD). You can customize the replacement string by specifying a substitute in the call to the EncoderReplacementFallback.EncoderReplacementFallback(String) constructor. After the substitute string is emitted, the encoding operation continues converting the remainder of the input.

  • Exception fallback, which throws an exception when a character cannot be encoded. The .NET Framework provides a predefined exception fallback handler. The EncoderExceptionFallback class throws an EncoderFallbackException when an invalid character is encountered, and the encoding operation terminates.

If you choose to implement a custom solution, you must override the following abstract members of the EncoderFallback class:

  • The CreateFallbackBuffer method, which returns a class instance derived from EncoderFallbackBuffer. Depending on the type of fallback handler that you are developing, the EncoderFallbackBuffer implementation is responsible for performing the mapping or replacement, or for throwing the exception.

  • The MaxCharCount property, which returns the maximum number of characters that the fallback implementation can return. For an exception fallback handler, its value should be zero.

For more information about encoding, decoding, and fallback strategies, see Character Encoding in the .NET Framework.

Constructors

EncoderFallback()

Initializes a new instance of the EncoderFallback class.

Properties

ExceptionFallback

Gets an object that throws an exception when an input character cannot be encoded.

MaxCharCount

When overridden in a derived class, gets the maximum number of characters the current EncoderFallback object can return.

ReplacementFallback

Gets an object that outputs a substitute string in place of an input character that cannot be encoded.

Methods

CreateFallbackBuffer()

When overridden in a derived class, initializes a new instance of the EncoderFallbackBuffer class.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also