0

我需要向包含以下 JSON 的服务器发布请求。我想在代表请求的类上使用 DataContractJsonSerializer 和 DataContract、DataMember 属性,例如

{"method":"mymethod","parameters":[10,"somestring"]}

这代表一个 RPC 调用

mymethod(10,"somestring"). 

在某些 API 中。API 中有许多具有不同参数列表的调用。

如果参数列表包含 T 类型的对象,我可以在其中使用 generic List<T>,这很简单,但 API 需要不同类型的参数列表(包括非原始对象)。

那么如何为参数数组构造 DataContract 呢?

4

2 回答 2

0

你需要做所有参数字符串类型 - 会有参数的序列化值。然后你需要这样做:

using System;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Json;
using System.Text;    
namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        string methodName = "test";
        MethodInfo methodInfo = typeof(ClassWithMethods).GetMethod(methodName);
        string[] parameters = new string[] { "1", "\"qwe\"", "{\"SomeProperty\":\"prop\"}" };
        object[] parameterValues = new object[parameters.Length];
        for (int i = 0; i < parameters.Length; i++)
        {
            DataContractJsonSerializer s = new DataContractJsonSerializer(methodInfo.GetParameters()[i].ParameterType);
            object p = s.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(parameters[i])));
            parameterValues[i] = p;
        }
        methodInfo.Invoke(new ClassWithMethods(), parameterValues);
    }
}

public class ClassWithMethods
{
    public void test(int i, string s, ComplexType ct)
    {
        Console.WriteLine("{0} {1} {2}", i, s, ct.SomeProperty);
    }
}


public class ComplexType
{
    public string SomeProperty { get; set; }
}
}
于 2013-11-29T12:36:09.110 回答
0

谢谢卢卡斯。所以稍微重述问题以包括复杂类型:-

{"method":"mymethod","parameters":[10,"somestring",{SomeProperty:value}]}

这表示对 JSON RPC 调用 mymethod(int,string,ComplexProperty) 的调用

The code is:-

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;

namespace JSONConsoleApplication4
{
    class Program
    {
        [DataContract]
        public class ComplexType
        {
            [DataMember]
            public string SomeProperty { get; set; }
        }

        [DataContract]
        public class GenericRequest
        {
            [DataMember]
            public string method { get; set; }
            [DataMember]
            public object[] parameters { get; set; }
        }


        static void Main(string[] args)
        {
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializerSettings settings = 
                new DataContractJsonSerializerSettings() { EmitTypeInformation=EmitTypeInformation.Never, 
                                                           KnownTypes=new Type[] { typeof(ComplexType) } };
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(GenericRequest),settings);
            serializer.WriteObject(ms, 
                new GenericRequest() { method = "mymethod", 
                                       parameters = new object[] { 10, "somestring", new ComplexType() { SomeProperty="value"}} });
            ms.Position = 0;
            string v = new StreamReader(ms).ReadToEnd();
        }
    }
}
于 2013-11-30T16:46:47.543 回答