I have a question which has bugged me a lot. I use JSON.Net to serialize/deserialize objects. I use this code to invoke methods via their parameter types.
If I run the following code I get an object[] {int, int}
Then I serialize/deserialize with Json.Net and after this process it turns to object[] {long, long}
Now my question: How can I alter the following code to preserve the type along with the values?
static class Program
{
static byte[] ObjectAsByteArray(object data)
{
string text = JsonConvert.SerializeObject(data, Formatting.None, new JsonSerializerSettings()
{
Formatting=Formatting.Indented,
TypeNameHandling = TypeNameHandling.All,
TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
});
return Encoding.UTF8.GetBytes(text);
}
static T ByteArrayAsObject<T>(byte[] data)
{
Object answer = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(data), typeof(T), new JsonSerializerSettings()
{
Formatting = Formatting.Indented,
TypeNameHandling = TypeNameHandling.All,
TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
});
return (T)answer;
}
static int Multiply(int a, int b)
{
return a * b;
}
static object[] ArgumentsOf(Expression<Action> expression)
{
MethodCallExpression outermostExpression = expression.Body as MethodCallExpression;
object[] Params = outermostExpression.Arguments.Cast<ConstantExpression>().Select(x => x.Value).ToArray();
return Params;
}
[STAThread]
static void Main()
{
object[] Arguments = ArgumentsOf(() => Multiply(5, 100));
byte[] ArgAsByte = ObjectAsByteArray(Arguments);
object[] DeserializedArguments = ByteArrayAsObject<object[]>(ArgAsByte);
}
}