System.Formats.Nrbf 9.0.0
About
System.Formats.Nrbf
exposes only one component: NrbfDecoder: a stateless, forward-only decoder class that can decode .NET Remoting Binary Format (NRBF) binary data from a stream.
You can think of NrbfDecoder as being the equivalent of using a JSON/XML reader without the deserializer.
Note: The 9.0.0 release of the System.Formats.Nrbf
package is marked [Experimental]
as the API shape is subject to change in the next major release. Even with the experimental annotation, the package is officially supported. Using the APIs from this package will produce a build warning with diagnostic ID SYSLIB5005
. The diagnostic can be suppressed with the acknowledgement that the API shape is subject to change in the next major release.
How to Use
The NRBF payload consists of serialization records that represent the serialized objects and their metadata. To read the whole payload and get the root record, you need to call one of the NrbfDecoder.Decode methods.
The Decode
method returns a SerializationRecord instance. SerializationRecord
is an abstract class that represents the serialization record and provides three properties: Id, RecordType, and TypeName. It exposes one method, TypeNameMatches, which compares the type name read from the payload (and exposed via TypeName
property) against the specified type. This method ignores assembly names, so you don't need to worry about type forwarding and assembly versioning. It also does not consider member names or their types (because getting this information would require type loading).
using System.Formats.Nrbf;
public class Sample
{
public int Integer;
public string? Text;
public byte[]? ArrayOfBytes;
public Sample? ClassInstance;
}
ClassRecord rootRecord = NrbfDecoder.DecodeClassRecord(payload);
Sample output = new()
{
// using the dedicated methods to read primitive values
Integer = rootRecord.GetInt32(nameof(Sample.Integer)),
Text = rootRecord.GetString(nameof(Sample.Text)),
// using dedicated method to read an array of bytes
ArrayOfBytes = ((SZArrayRecord<byte>)rootRecord.GetArrayRecord(nameof(Sample.ArrayOfBytes))).GetArray(),
// using GetClassRecord to read a class record
ClassInstance = new()
{
Text = rootRecord
.GetClassRecord(nameof(Sample.ClassInstance))!
.GetString(nameof(Sample.Text))
}
};
Main Types
There are more than a dozen different serialization record types. This library provides a set of abstractions, so you only need to learn a few of them:
PrimitiveTypeRecord<T>
: describes all primitive types natively supported by the NRBF (string
,bool
,byte
,sbyte
,char
,short
,ushort
,int
,uint
,long
,ulong
,float
,double
,decimal
,TimeSpan
, andDateTime
).- Exposes the value via the
Value
property. PrimitiveTypeRecord<T>
derives from the non-generic PrimitiveTypeRecord, which also exposes a Value property. But on the base class, the value is returned asobject
(which introduces boxing for value types).
- Exposes the value via the
- ClassRecord: describes all
class
andstruct
besides the aforementioned primitive types. - ArrayRecord: describes all array records, including jagged and multi-dimensional arrays.
SZArrayRecord<T>
: describes single-dimensional, zero-indexed array records, whereT
can be either a primitive type or aSerializationRecord
.
SerializationRecord rootObject = NrbfDecoder.Decode(payload); // payload is a Stream
if (rootObject is PrimitiveTypeRecord primitiveRecord)
{
Console.WriteLine($"It was a primitive value: '{primitiveRecord.Value}'");
}
else if (rootObject is ClassRecord classRecord)
{
Console.WriteLine($"It was a class record of '{classRecord.TypeName.AssemblyQualifiedName}' type name.");
}
else if (rootObject is SZArrayRecord<byte> arrayOfBytes)
{
Console.WriteLine($"It was an array of `{arrayOfBytes.Length}`-many bytes.");
}
Additional Documentation
Feedback & Contributing
System.Formats.Nrbf is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.
Showing the top 20 packages that depend on System.Formats.Nrbf.
Packages | Downloads |
---|---|
System.Resources.Extensions
Provides classes which read and write resources in a format that supports non-primitive objects.
Commonly Used Types:
System.Resources.Extensions.DeserializingResourceReader
System.Resources.Extensions.PreserializedResourceWriter
|
22 |
System.Resources.Extensions
Provides classes which read and write resources in a format that supports non-primitive objects.
Commonly Used Types:
System.Resources.Extensions.DeserializingResourceReader
System.Resources.Extensions.PreserializedResourceWriter
|
20 |
System.Resources.Extensions
Provides classes which read and write resources in a format that supports non-primitive objects.
Commonly Used Types:
System.Resources.Extensions.DeserializingResourceReader
System.Resources.Extensions.PreserializedResourceWriter
|
17 |
System.Resources.Extensions
Provides classes which read and write resources in a format that supports non-primitive objects.
Commonly Used Types:
System.Resources.Extensions.DeserializingResourceReader
System.Resources.Extensions.PreserializedResourceWriter
|
7 |
System.Resources.Extensions
Provides classes which read and write resources in a format that supports non-primitive objects.
Commonly Used Types:
System.Resources.Extensions.DeserializingResourceReader
System.Resources.Extensions.PreserializedResourceWriter
|
2 |
.NET Framework 4.6.2
- System.Reflection.Metadata (>= 9.0.0)
- Microsoft.Bcl.HashCode (>= 1.1.1)
- System.ValueTuple (>= 4.5.0)
.NET 8.0
- System.Reflection.Metadata (>= 9.0.0)
.NET 9.0
- No dependencies.
.NET Standard 2.0
- System.Reflection.Metadata (>= 9.0.0)
- Microsoft.Bcl.HashCode (>= 1.1.1)
Version | Downloads | Last updated |
---|---|---|
9.0.0 | 1 | 11/17/2024 |
9.0.0-rc.2.24473.5 | 6 | 10/26/2024 |
9.0.0-rc.1.24431.7 | 16 | 9/13/2024 |
9.0.0-preview.7.24405.7 | 18 | 8/18/2024 |
9.0.0-preview.6.24327.7 | 21 | 7/9/2024 |