Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# object to string and back

Tags:

string

c#

object

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 remote 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.

like image 240
Kirk Avatar asked Aug 08 '11 08:08

Kirk


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C language?

C is a high-level and general-purpose programming language that is ideal for developing firmware or portable applications. Originally intended for writing system software, C was developed at Bell Labs by Dennis Ritchie for the Unix Operating System in the early 1970s.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

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);
}
like image 195
Aharon Ohayon Avatar answered Sep 19 '22 13:09

Aharon Ohayon