Reputation: 31
I am playing around with the C# protobuf implementation and I might have run into a problem. I want to deserialize the result dynamically.
public byte[] ManageRequest(string argument1, params FunctionalParameter[] argument2)
{
var serverBase = new ServerBase();
if (argument1 != null)
{
MethodInfo type = serverBase.GetType().GetMethod(argument1);
ParameterInfo[] parameters = type.GetParameters();
if (parameters.Length.Equals(argument2.Length))
{
var pars = new object[argument2.Length];
for (int i = 0; i < parameters.Length; i++)
{
if (parameters[i].Name == argument2[i].ParameterNameField)
{
using (Stream result = new MemoryStream(argument2[i].ParameterValueField))
{
pars[i] = Serializer.Deserialize<int>(result); // I want to deserialize the //result dynamically( not want to use int, we can pass result dynamically)
}
}
}
if (pars.Length.Equals(argument2.Length))
{
return type.Invoke(serverBase, pars).ToProtoBuf();
}
}
}
return null;
}
The method takes two arguments:
1. argument1
- MethodName
2. argument2
- Array of FunctionalParameter
s (In this class we have two members:
string ParameterNameField
and byte[] ParameterValueField
.)
I want to Deserialize the result dynamically, e.g Serializer.Deserialize<int>(result);
I have passed int to serialize, in this case I know that we need to deserialize the result in int type but I want to deserialize the result dynamically that I have serialized.
Using parameters[i].ParameterType.Name
code I got the class name (e.g System.Int32
, etc) and user defind datatype, like Class1
, Class2
, etc. If I passed Serializer.Deserialize <parameters[i].GetType()> (result)
in this case I got the following error:
Cannot apply
operator '<'
to operands of type 'method group
' and 'System.Type
'
Protobuf.Serializer
is a class in protobuf-net.dll. In this class, Serialize<T>(System.IO.Stream)
and Deserialize<T>(System.IO.Stream)
are two methods to serialize and deserialize the result.
I do not understand that how this is possible! Can you please explain it to me?
Upvotes: 1
Views: 1060
Reputation: 31
Thanks.. Using this, I got the result that I have want ... I wrote some code that Deserialize the result dynamically Here is my code...
if (parameters[i].Name == argument2[i].ParameterNameField)
{
using (Stream result = new MemoryStream(argument2[i].ParameterValueField))
{
pars[i] = Serializer.NonGeneric.Deserialize(parameters[i].ParameterType, result);
}
}
parameters[i].ParameterType - Get type of the parameter and Deserialize the same. Thanks again. But I getting confused as per your Answer "As a side note: in the v1.* builds, the primary API is generic; the non-generic API had to do some extra work (and had some extra overhead). In the v2.* builds, the core codebase is non-generic." Is this code take extra overhead ?
Upvotes: -2
Reputation: 1062975
The Serializer.Deserialize<T>(...)
API is generic. There is, however, a non-generic API... Serializer.NonGeneric.Deserialize(...)
that accepts a Type
. It sounds like you want to use this latter API.
As a side note: in the v1.* builds, the primary API is generic; the non-generic API had to do some extra work (and had some extra overhead). In the v2.* builds, the core codebase is non-generic. The generic API doesn't have additional overhead from this, as it can just use typeof(T)
etc.
Upvotes: 4