开发者

C# object to string and back

开发者 https://www.devze.com 2023-03-26 20:34 出处:网络
My problem: I have a dynamic codecompiler which can compile a snippet of code. The rest of the code. (imports, namespace, class, main function) is already there. The snippet get inserted into that and

My problem:

I have a dynamic codecompiler which can compile a snippet of code. The rest of the code. (imports, namespace, class, main function) is already there. The snippet get inserted into that and then it is compiled to an assembly and executed. This is how user is able execute code snippet. The main function (where the snippet is executed) has a return type of object. This snippet gets executed on a r开发者_高级运维emote computer. The code is send by the client to a webserver. The remote computer reads out the code from the webserver and executes it. On the remote computer I can easily view the type of the returned object and its value. However I can only send strings to the webserver.

Question:

How do I convert a object into a string, no matter what the type is and how do I convert it back?

Tried:

I tried using ToString(), that works fine when using int, string, double and bool. But with an image or an other type is doesn't work of course because I also need to able to convert it back.


Serialize the object using the BinaryFormatter, and then return the bytes as a string (Base64 encoded). Doing it backwards gives you your object back.

public string ObjectToString(object obj)
{
   using (MemoryStream ms = new MemoryStream())
   {
     new BinaryFormatter().Serialize(ms, obj);         
     return Convert.ToBase64String(ms.ToArray());
   }
}

public object StringToObject(string base64String)
{    
   byte[] bytes = Convert.FromBase64String(base64String);
   using (MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length))
   {
      ms.Write(bytes, 0, bytes.Length);
      ms.Position = 0;
      return new BinaryFormatter().Deserialize(ms);
   }
}


It's an old question but I think that I have a solution that can work better for most of the times (it creates a shorter string and doesn't require the Serializable attribute).

The idea is to serialize the object to JSON and then convert it to base64, see the extension function:

public static string ToBase64(this object obj)
{
    string json = JsonConvert.SerializeObject(obj);

    byte[] bytes = Encoding.Default.GetBytes(json);

    return Convert.ToBase64String(bytes);
}

In order to create the object, we need to convert the base64 to bytes, convert to string and deserialize the JSON to T

public static T FromBase64<T>(this string base64Text)
{
    byte[] bytes = Convert.FromBase64String(base64Text);

    string json = Encoding.Default.GetString(bytes);

    return JsonConvert.DeserializeObject<T>(json);
}


You will have to make a conversion method to display it and serialize it to be able to convert it back and forth.

For instance:

    public static string ConvertToDisplayString(object o)
    {
        if (o == null)
            return "null";
        var type = o.GetType();
        if (type == typeof(YourType))
        {
            return ((YourType)o).Property;
        }
        else
        {
            return o.ToString();
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消