plutoo / protobuf-csharp-port

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

Deserialize the result dynamically #53

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I am playing around with the C# protobuf 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)
                                }
                            }
                    }
                    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 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 
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'"

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 6:05

GoogleCodeExporter commented 9 years ago
What is Serializer here? Are you sure you're using protobuf-csharp-port rather 
than protobuf-net?

Original comment by jonathan.skeet on 28 Mar 2013 at 7:06

GoogleCodeExporter commented 9 years ago
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 
result.

Original comment by aady78...@gmail.com on 28 Mar 2013 at 8:49

GoogleCodeExporter commented 9 years ago
protobuf-net.dll isn't part of the protobuf-csharp-port project.

As I suggested on the previous comment, you're looking at the wrong project. 
You're using protobuf-net, so you should be asking for a feature request (which 
is what this is, not a defect btw) on that project:

http://protobuf-net.googlecode.com

Original comment by jonathan.skeet on 28 Mar 2013 at 10:42