1

I'm converting legacy C# code to Unity 2017, and I'm stuck on methods to read binary data to a large struct.

I have the following struct defined :

[StructLayout(LayoutKind.Sequential)]
struct Header
{
    public readonly int Magic;
    public readonly int Version;
    public readonly int FieldA;
    public readonly int FieldB;
    // ...
}

and the following code from my legacy C# application :

using (var strm = new FileStream(mFileName, FileMode.Open))
    {
    var reader = new BinaryReader(strm);
    mHeader = reader.Read<Header>(); // <-- error here
    }

The error message is : The non-generic method 'BinaryReader.Read()' cannot be used with typed arguments

I guess that a workaround could be to use a bunch of reader.Read() calls, but the header struct is rather large and I'd like to find a way to use what looks like a good shortcut and keep the code simpler and smaller.

5
  • 2
    That Read<T>() method is custom code. It has never been part of the framework. It's probably an extension method, and if so, just copy it. Commented Jan 2, 2018 at 19:11
  • Well, where may I find it ? I didn't see anything in the file defining the Header struct, so any hint for finding this code ? (I'm a C# newbie as you can see) Commented Jan 2, 2018 at 19:14
  • In the original code, click somewhere on "Read", then hit F12 or right click it and go to implementation. Commented Jan 2, 2018 at 19:19
  • The original code is not in an IDE which I could use, but with your help, I finally manage to find an Extensions.cs containing the Read method. Many thanks !!! Commented Jan 2, 2018 at 19:21
  • That works too. Commented Jan 2, 2018 at 19:21

1 Answer 1

1

That Read() method is custom code. It has never been part of the framework. It's probably an extension method, and if so, just copy it.

You can find it by clicking somewhere on "Read", then hit F12 or right click it and go to implementation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.