BencodeStringEnumConverter<TEnum> Class
Definition
- Namespace
- Bodu.Text.Bencode.Serialization
- Assembly
- Bodu.Text.Bencode.dll
- Package
- Bodu.Text.Bencode 1.3.0
Produces a converter that serializes the enumeration TEnum as a Bencode byte string holding
its member name, applying an optional naming policy and honoring StringEnumMemberNameAttribute on
individual members.
public sealed class BencodeStringEnumConverter<TEnum> : BencodeConverterFactory where TEnum : struct, Enum
Type Parameters
TEnumThe enumeration type the produced converter handles.
- Inheritance
-
BencodeStringEnumConverter<TEnum>
- Inherited Members
- Extension Methods
Examples
[Converter(typeof(BencodeStringEnumConverter<Status>))]
public enum Status
{
Active,
OnHold,
}
// Status.OnHold serializes as the byte string 6:OnHold.
Remarks
Unlike the non-generic BencodeStringEnumConverter, this strongly typed factory can be referenced from
a ConverterAttribute on a member, property, or the enumeration itself, because it exposes a public
parameterless constructor and applies to a single enumeration type. An integer accepted on read must lie within the
range of the underlying type of TEnum; one outside it throws
BencodeSerializationException rather than being narrowed into that type.
Names are matched as System.Text.Json's JsonStringEnumConverter matches them. A member's wire name, from
StringEnumMemberNameAttribute or the naming policy, binds only in its own case; the CLR name of a
member the attribute does not rename also binds, in any case, while a renamed member's CLR name does not. A
combination of flags is written as the comma-separated wire names of its flags, in the order
ToString() lists them, and is read from a list of names in any order, with a trailing comma
tolerated; an integer binds, where integers are allowed, only as the whole text.
Like System.Text.Json's JsonStringEnumConverter from .NET 9, a converter that does not allow integers throws
BencodeSerializationException on a Bencode integer, and at a byte string that is not a member's wire
name and has an integer in any of its comma-separated parts, such as 2 or Read, 2. A member whose wire
name is an integer's text still reads, because the wire names are matched first. Such a converter also refuses to
write a value that is neither a member nor a combination of defined flags, such as an undefined value or a flags
value with an undefined bit, and throws BencodeSerializationException rather than writing the decimal
text it would reject on read.
Constructors
BencodeStringEnumConverter()
Initializes a new instance of the BencodeStringEnumConverter<TEnum> class that applies no naming policy and accepts integers on read.
public BencodeStringEnumConverter()
BencodeStringEnumConverter(NamingPolicy?, bool)
Initializes a new instance of the BencodeStringEnumConverter<TEnum> class with the specified naming policy and integer-handling behavior.
public BencodeStringEnumConverter(NamingPolicy? namingPolicy, bool allowIntegerValues)
Parameters
namingPolicyNamingPolicyThe naming policy applied to member names, or null to use member names unchanged.
allowIntegerValuesboolWhether integer values are allowed: read from a Bencode integer or a byte string that holds one, and written as the decimal text of a value that is neither a member nor a combination of flags.
Methods
CanConvert(Type)
Determines whether the converter can convert the specified type.
public override bool CanConvert(Type typeToConvert)
Parameters
typeToConvertTypeThe type to test.
Returns
CreateConverter(Type, BencodeSerializerOptions)
Creates a converter for the specified type.
public override BencodeConverter CreateConverter(Type typeToConvert, BencodeSerializerOptions options)
Parameters
typeToConvertTypeThe closed type to create a converter for.
optionsBencodeSerializerOptionsThe serializer options in effect.
Returns
- BencodeConverter
A converter that handles
typeToConvert.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |