using System.IO; namespace ReadStructExample { struct Person { public int age; public string name; } class Program { static void Main(string[] args) { Person person; using (BinaryReader reader = new BinaryReader(File.Open("person.bin", FileMode.Open))) { person.age = reader.ReadInt32(); person.name = reader.ReadString(); } Console.WriteLine("Name: " + person.name + ", Age: " + person.age); } } }
using System; using System.IO; using System.Runtime.InteropServices; namespace ReadStructExample { [StructLayout(LayoutKind.Sequential)] struct Vector3 { public float x; public float y; public float z; } class Program { static void Main(string[] args) { Vector3 vector; using (BinaryReader reader = new BinaryReader(File.Open("vector.bin", FileMode.Open))) { vector = reader.ReadStructIn this example, a `Vector3` struct is defined with three `float` fields. The `StructLayout` attribute is used to specify the memory layout of the struct. The `ReadStruct` method is used to read the binary data from a file called `vector.bin` into a `Vector3` object, which is then printed to the console. The `ReadStruct` method seems to be a custom extension method, as it is not included in the official `System.IO` namespace. It could be part of a custom library or package.(); } Console.WriteLine("x: " + vector.x + ", y: " + vector.y + ", z: " + vector.z); } } }