heweitykc / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
0 stars 0 forks source link

Deserialize the result dynamically #363

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I am playing around with the C# protobuf-net implementation and I might have 
run into a problem.

I want to deserialize the result dynamically

Please looking at the Code,

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)
                // e.g Serializer.Deserialize <parameters[i].GetType()> (result);
                                }
                            }
                    }
                    if (pars.Length.Equals(argument2.Length))
                    {
                        return type.Invoke(serverBase, pars).ToProtoBuf();
                    }
                }
            }
            return null;
        }

Methods arguments,
1) argument1 - MethodName 
2) argument2 - Array of FunctionalParameter class ( In this class we have two 
members 
(a)String ParameterNameField  (b) byte[] ParameterValueField.)

My question is I want to Deserialize the result dynamically 
e.g Serializer.Deserialize<int>(result); I have passed int(hard coded) 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 
intially.

using "parameters[i].ParameterType.Name" code I got the Class name e.g System 
datatype i.e System.Int32 etc and user defind datatype ie Class1, class2 etc

If I passed Serializer.Deserialize <parameters[i].GetType()> (result) in this 
case I got error "Cannot apply operator '<' to operands of type 'method group' 
and System.Type'"

Protobuf.Serializer is a class in protobuf-net.dll.
Here we have Serialize<T>(System.IO.Stream) and  
Deserialize<T>(System.IO.Stream) are two methods to serialize and deserialize 
result.

I do not understand that How is it possible ?.

Please provide some help,
Thanks in Advance

Aditya

Original issue reported on code.google.com by aady78...@gmail.com on 28 Mar 2013 at 11:40

GoogleCodeExporter commented 9 years ago
Asked and answered on stackoverflow 
http://stackoverflow.com/questions/15678207/deserialize-the-result-dynamically-f
or-protobuf-net-dll

Original comment by marc.gravell on 28 Mar 2013 at 3:02

GoogleCodeExporter commented 9 years ago
Asked and answered on stackoverflow 
http://stackoverflow.com/questions/15678207/deserialize-the-result-dynamically-f
or-protobuf-net-dll

Original comment by marc.gravell on 28 Mar 2013 at 3:02

GoogleCodeExporter commented 9 years ago
Asked and answered on stackoverflow 
http://stackoverflow.com/questions/15678207/deserialize-the-result-dynamically-f
or-protobuf-net-dll

Original comment by marc.gravell on 28 Mar 2013 at 3:02

GoogleCodeExporter commented 9 years ago
Asked and answered on stackoverflow 
http://stackoverflow.com/questions/15678207/deserialize-the-result-dynamically-f
or-protobuf-net-dll

Original comment by marc.gravell on 28 Mar 2013 at 3:02