How to convert byte array to an object in C#
By FoxLearn 1/20/2025 9:15:13 AM 299
To convert a byte array back to an object in C#, you can use deserialization.
How to convert byte array to an object in C#?
For example:
public static T FromByteArray<T>(byte[] data) { if (data != null) { using (MemoryStream ms = new MemoryStream(data)) { object obj = new BinaryFormatter().Deserialize(ms); return (T)obj; } } return default(T); }
Usage
[Serializable] public class People { public int Id { get; set; } public string Name { get; set; } } //Main.cs byte[] byteArray = /* your byte array here */; People obj = ByteArrayToObject<People>(byteArray);
When using BinaryFormatter
, make sure the class is marked with [Serializable]
.
- Capturing screenshots in C#
- ChromeDevToolsSystemMenu does not exist in the current context in CefSharp
- How to download a webfile in C#
- How to disable the native context menu in CefSharp
- Creating a scanning application in C#
- How to generate a PDF from HTML using wkhtmltopdf in C#
- How to retrieve the Executable Path in C#
- How to Convert string to JSON in C#
Categories
Popular Posts
DASHMIN Admin Dashboard Template
11/17/2024
Freedash bootstrap lite
11/13/2024