
Serialization/Deserialization with Codemations.Asn1
Example class 1:
[AsnSequence]
public class FooQuestion
{
[AsnTag]
public BigInteger TrackingNumber { get; set; }
[AsnTag]
public string Question { get; set; }
}
Serialization:
var myQuestion = new FooQuestion
{
TrackingNumber = 5,
Question = "Anybody there?"
};
AsnSerializer.Serialize(myQuestion, AsnEncodingRules.DER);
Output (hex):
30, 13,
02, 01, 05,
16, 0e, 41, 6e, 79, 62, 6f, 64, 79, 20, 74, 68, 65, 72, 65, 3f
Deserialization:
var encodedData = new byte[] {
0x30, 0x13, 0x02, 0x01, 0x05, 0x16, 0x0e, 0x41, 0x6e, 0x79, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x68, 0x65, 0x72, 0x65, 0x3f
};
AsnSerializer.Deserialize<FooQuestion>(encodedData, AsnEncodingRules.DER);
Built-in types mapping
| .NET Type | ASN.1 Type | Description |
|---|
bool | BOOLEAN | Represents a true or false value. |
byte | INTEGER | Unsigned integer (8-bit). |
sbyte | INTEGER | Signed integer (8-bit). |
short | INTEGER | Signed integer (16-bit). |
|