Show / Hide Table of Contents

Class BinaryReaderExtensions

Contains useful extensions for the BinaryReader class.

Inheritance
object
BinaryReaderExtensions
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: TheXDS.MCART.Types.Extensions
Assembly: MCART.dll
Syntax
public static class BinaryReaderExtensions

Methods

| Edit this page View Source

GetBinaryReadMethod(Type)

Gets a read method defined in the BinaryReader class that can be used to read an object of the specified type.

Declaration
public static MethodInfo? GetBinaryReadMethod(Type t)
Parameters
Type Name Description
Type t

Type for which to get a read method defined in the BinaryReader class.

Returns
Type Description
MethodInfo

A read method defined in the BinaryReader class that can be used to read an object of the specified type, or null if no read method exists for the specified type.

| Edit this page View Source

MarshalReadArray<T>(BinaryReader, in int)

Reads an array of type T by marshaling.

Declaration
public static T[] MarshalReadArray<T>(this BinaryReader br, in int count) where T : struct
Parameters
Type Name Description
BinaryReader br

BinaryReader to be used for reading.

int count

Number of elements to be read.

Returns
Type Description
T[]

An array of T containing count elements.

Type Parameters
Name Description
T

Type of elements to be read. Must be a struct readable by marshaling.

| Edit this page View Source

MarshalReadArray<T>(BinaryReader, in long, in int)

Moves to a specific location in the Stream being read by the BinaryReader and reads an array of type T by marshaling.

Declaration
public static T[] MarshalReadArray<T>(this BinaryReader br, in long offset, in int count) where T : struct
Parameters
Type Name Description
BinaryReader br

BinaryReader to be used for reading.

long offset

Offset from which to start reading the array.

int count

Number of elements to be read.

Returns
Type Description
T[]

An array of T containing count elements.

Type Parameters
Name Description
T

Type of elements to be read. Must be a struct readable by marshaling.

| Edit this page View Source

MarshalReadStruct<T>(BinaryReader)

Reads a simple structure from the reader using marshaling.

Declaration
public static T MarshalReadStruct<T>(this BinaryReader reader) where T : struct
Parameters
Type Name Description
BinaryReader reader

Instance of BinaryReader from which to perform the read.

Returns
Type Description
T

The value read from reader.

Type Parameters
Name Description
T

Type of value to read.

Remarks

This function differs from Read<T>(BinaryReader) in that the read will be performed exclusively using marshaling techniques, without including the default functions and/or those implemented in the BinaryReader class or a known extension method of it.

This method also requires that strings and arrays be annotated with the MarshalAsAttribute, setting a constant maximum expected size for the string or array. To read structures with strings or arrays of arbitrary size, consider using ReadStruct<T>(BinaryReader).

See Also
Read<T>(BinaryReader)
ReadStruct<T>(BinaryReader)
| Edit this page View Source

Read(BinaryReader, Type)

Reads a value of the specified type from the reader.

Declaration
public static object Read(this BinaryReader reader, Type type)
Parameters
Type Name Description
BinaryReader reader

Instance of BinaryReader from which to read.

Type type

Type of value to read.

Returns
Type Description
object

The value read from reader.

Remarks

If you know the exact type of the value to read and a method exists in the BinaryReader class or an extension is defined for reading that value, prefer using that method or extension for better performance. Additionally, if the value to read is a simple user-defined structure, you can use the MarshalReadStruct<T>(BinaryReader) method to read a structure using marshaling, which may provide better performance than using this method.

Also, note that this method will read structures by reading the values of their fields, including both private and public fields (and therefore, properties).

Exceptions
Type Condition
InvalidOperationException

Thrown if no read method exists for the specified type.

See Also
MarshalReadStruct<T>(BinaryReader)
ReadStruct<T>(BinaryReader)
| Edit this page View Source

ReadArray(BinaryReader, Type)

Reads an array of type arrayType from the reader.

Declaration
public static Array ReadArray(this BinaryReader reader, Type arrayType)
Parameters
Type Name Description
BinaryReader reader

Instance of BinaryReader from which to perform the read.

Type arrayType

Type of array to read.

Returns
Type Description
Array

An Array of the specified type that has been read from the specified BinaryReader.

| Edit this page View Source

ReadArray(BinaryReader, Type, int)

Reads an array of type elementType from the reader.

Declaration
public static Array ReadArray(this BinaryReader reader, Type elementType, int dimensions)
Parameters
Type Name Description
BinaryReader reader

Instance of BinaryReader from which to perform the read.

Type elementType

Type of elements in the array.

int dimensions

Number of dimensions of the array.

Returns
Type Description
Array

An Array with the specified dimensions and whose elements are of the specified type that has been read from the specified BinaryReader.

| Edit this page View Source

ReadArray<T>(BinaryReader)

Reads a one-dimensional array from the reader.

Declaration
public static T[] ReadArray<T>(this BinaryReader reader)
Parameters
Type Name Description
BinaryReader reader

Instance of BinaryReader from which to perform the read.

Returns
Type Description
T[]

A one-dimensional array that has been read from the specified BinaryReader, whose elements are of the specified type.

Type Parameters
Name Description
T

Type of elements in the array.

| Edit this page View Source

ReadBytesAt(BinaryReader, in long, in int)

Reads an array of bytes starting on the specified offset.

Declaration
public static byte[] ReadBytesAt(this BinaryReader reader, in long offset, in int count)
Parameters
Type Name Description
BinaryReader reader

BinaryReader to be used for reading.

long offset

Offset from which to start reading the array.

int count

Number of bytes to be read.

Returns
Type Description
byte[]

An array with count bytes that has been read starting from offset.

| Edit this page View Source

ReadDateTime(BinaryReader)

Reads a DateTime.

Declaration
public static DateTime ReadDateTime(this BinaryReader br)
Parameters
Type Name Description
BinaryReader br

BinaryReader from which to read.

Returns
Type Description
DateTime

DateTime read from the BinaryReader.

| Edit this page View Source

ReadEnum(BinaryReader, Type)

Reads an enumeration value.

Declaration
public static Enum ReadEnum(this BinaryReader br, Type enumType)
Parameters
Type Name Description
BinaryReader br

BinaryReader from which to read.

Type enumType

Type of enumeration value to read.

Returns
Type Description
Enum

Enumeration value read from the BinaryReader.

| Edit this page View Source

ReadEnum<T>(BinaryReader)

Reads an enumeration value.

Declaration
public static T ReadEnum<T>(this BinaryReader br) where T : struct, Enum
Parameters
Type Name Description
BinaryReader br

BinaryReader from which to read.

Returns
Type Description
T

Enumeration value read from the BinaryReader.

Type Parameters
Name Description
T

Type of enumeration value to read.

| Edit this page View Source

ReadGuid(BinaryReader)

Reads a Guid.

Declaration
public static Guid ReadGuid(this BinaryReader br)
Parameters
Type Name Description
BinaryReader br

BinaryReader from which to read.

Returns
Type Description
Guid

Guid read from the BinaryReader.

| Edit this page View Source

ReadNullTerminatedString(BinaryReader)

Reads a null-terminated string ('\0') using UTF8 by default.

Declaration
public static string ReadNullTerminatedString(this BinaryReader br)
Parameters
Type Name Description
BinaryReader br

BinaryReader from which to read the string.

Returns
Type Description
string

A null-terminated string ('\0') read from the BinaryReader.

| Edit this page View Source

ReadNullTerminatedString(BinaryReader, Encoding)

Reads a null-terminated string ('\0').

Declaration
public static string ReadNullTerminatedString(this BinaryReader br, Encoding encoding)
Parameters
Type Name Description
BinaryReader br

BinaryReader from which to read the string.

Encoding encoding

Encoding to use for reading the string.

Returns
Type Description
string

A null-terminated string ('\0') read from the BinaryReader.

| Edit this page View Source

ReadStruct<T>(BinaryReader)

Reads a simple structure from the reader.

Declaration
public static T ReadStruct<T>(this BinaryReader reader) where T : struct
Parameters
Type Name Description
BinaryReader reader

Instance of BinaryReader from which to perform the read.

Returns
Type Description
T

The value read from reader.

Type Parameters
Name Description
T

Type of value to read.

Remarks

This method allows reading structures based on their fields, both private and public (and therefore, including properties with backing fields), as long as they are writable, i.e., not declared as readonly.

If the structure does not contain arbitrarily-sized strings or arrays, or those have been annotated with the MarshalAsAttribute attribute setting the SizeConst property, consider using the MarshalReadStruct<T>(BinaryReader) method, as it may provide better performance.

See Also
Read<T>(BinaryReader)
MarshalReadStruct<T>(BinaryReader)
| Edit this page View Source

ReadTimeSpan(BinaryReader)

Reads a TimeSpan.

Declaration
public static TimeSpan ReadTimeSpan(this BinaryReader br)
Parameters
Type Name Description
BinaryReader br

BinaryReader from which to read.

Returns
Type Description
TimeSpan

TimeSpan read from the BinaryReader.

| Edit this page View Source

Read<T>(BinaryReader)

Reads a value of the specified type from the reader.

Declaration
public static T Read<T>(this BinaryReader reader)
Parameters
Type Name Description
BinaryReader reader

Instance of BinaryReader from which to read.

Returns
Type Description
T

The value read from reader.

Type Parameters
Name Description
T

Type of value to read.

Remarks

If you know the exact type of the value to read and a method exists in the BinaryReader class or an extension is defined for reading that value, prefer using that method or extension for better performance. Additionally, if the value to read is a simple user-defined structure, you can use the MarshalReadStruct<T>(BinaryReader) method to read a structure using marshaling, which may provide better performance than using this method.

Exceptions
Type Condition
InvalidOperationException

Thrown if no read method exists for the specified type.

See Also
MarshalReadStruct<T>(BinaryReader)
  • Edit this page
  • View Source
In this article
Back to top Generated by DocFX