Tags
Asked 2 years ago
9 Jun 2021
Views 326
eclipse-learner

eclipse-learner posted

PHP like serialized string in .NET

PHP like serialized string in .NET
Phpworker

Phpworker
answered May 1 '23 00:00

In PHP, serialized strings can be generated using the serialize() function , which converts a PHP value into a string representation that can be stored or transmitted. Similarly, the unserialize() function can be used to convert the serialized string back into its original PHP value .

In .NET, a similar functionality can be achieved using the DataContractSerializer class , which is part of the System.Runtime.Serialization namespace. This class allows you to serialize and deserialize objects into XML or binary format.

Here's an example of serializing an object into a binary format in .NET:



using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

[Serializable]
public class MyClass
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void Main()
{
    // Create an instance of the class to serialize
    MyClass myObject = new MyClass();
    myObject.Name = "John Doe";
    myObject.Age = 30;

    // Create a BinaryFormatter object to perform the serialization
    BinaryFormatter formatter = new BinaryFormatter();

    // Create a MemoryStream to store the serialized data
    MemoryStream stream = new MemoryStream();

    // Use the formatter to serialize the object into the stream
    formatter.Serialize(stream, myObject);

    // Convert the serialized data into a byte array
    byte[] serializedData = stream.ToArray();

    // Display the serialized data as a string
    Console.WriteLine(Convert.ToBase64String(serializedData));
}

In this example, we define a MyClass class with two properties, Name and Age. We then create an instance of this class and serialize it into a binary format using a BinaryFormatter object and a MemoryStream. Finally, we convert the serialized data into a byte array and display it as a string using the Convert.ToBase64String() method.

To deserialize the serialized data back into its original object, you can use the Deserialize() method of the BinaryFormatter object:


// Convert the serialized data from a string back into a byte array
byte[] serializedData = Convert.FromBase64String(serializedString);

// Create a MemoryStream from the byte array
MemoryStream stream = new MemoryStream(serializedData);

// Create a BinaryFormatter object to perform the deserialization
BinaryFormatter formatter = new BinaryFormatter();

// Deserialize the object from the stream
MyClass myObject = (MyClass)formatter.Deserialize(stream);

In this example, we first convert the serialized data from a string back into a byte array using the Convert.FromBase64String() method. We then create a MemoryStream object from the byte array and use a BinaryFormatter object to deserialize the object from the stream. Finally, we cast the deserialized object to the MyClass type.
Post Answer