C# 反映WCF';s的反序列化JSON类
我遇到了一个有趣的问题 想象一下这样的简单类层次结构:C# 反映WCF';s的反序列化JSON类,c#,.net,json,wcf,reflection,C#,.net,Json,Wcf,Reflection,我遇到了一个有趣的问题 想象一下这样的简单类层次结构: class ClassA { public ClassB RefClassB { get; set; } public ClassA() { RefClassB = new ClassB(); } } class ClassB { public ClassC RefClassC { get; set; } public ClassB() { Ref
class ClassA
{
public ClassB RefClassB { get; set; }
public ClassA()
{
RefClassB = new ClassB();
}
}
class ClassB
{
public ClassC RefClassC { get; set; }
public ClassB()
{
RefClassC = new ClassC();
}
}
class ClassC
{
public int IntProperty { get; set; }
}
namespace AeviGateway.Service
{
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, Method = "POST", BodyStyle=WebMessageBodyStyle.Bare)]
MyModel.JSONResponse MyMethod(MyModel.JSONRequest data);
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Service: IService
{
public MyModel.JSONResponse MyMethod(MyModel.JSONRequest data)
{
GetProperties(data); // Here is the problem - it seems like all subclasses would be null..
....
}
}
}
{
"header": {
"version": "1",
"token": "d1c2c5b4-c5c6-4a31-89a5-75d4db4acaae"
},
"request": {
"number": 10,
"SubObject": {
"UID": "320f324e-22cd-4bd3-9e58-d965749d1986",
"Number": 11,
"Text": "something"
}
}
}
…这段代码使用.NET Reflection
private void button1_Click(object sender, EventArgs e)
{
ClassA instanceOfClassA = new ClassA();
GetProperties(c);
}
private void GetProperties<T>(T instance)
{
GetProperties(typeof(T), instance);
}
private void GetProperties(Type classType, object instance)
{
foreach (PropertyInfo property in classType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
object val = property.GetValue(instance, null);
if (val == null)
{
// e.g. I put to text box property names
textBox1.Text += property.Name + Environment.NewLine;
}
else
{
GetProperties(property.PropertyType, val);
}
}
}
JSON是这样的:
class ClassA
{
public ClassB RefClassB { get; set; }
public ClassA()
{
RefClassB = new ClassB();
}
}
class ClassB
{
public ClassC RefClassC { get; set; }
public ClassB()
{
RefClassC = new ClassC();
}
}
class ClassC
{
public int IntProperty { get; set; }
}
namespace AeviGateway.Service
{
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, Method = "POST", BodyStyle=WebMessageBodyStyle.Bare)]
MyModel.JSONResponse MyMethod(MyModel.JSONRequest data);
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Service: IService
{
public MyModel.JSONResponse MyMethod(MyModel.JSONRequest data)
{
GetProperties(data); // Here is the problem - it seems like all subclasses would be null..
....
}
}
}
{
"header": {
"version": "1",
"token": "d1c2c5b4-c5c6-4a31-89a5-75d4db4acaae"
},
"request": {
"number": 10,
"SubObject": {
"UID": "320f324e-22cd-4bd3-9e58-d965749d1986",
"Number": 11,
"Text": "something"
}
}
}
…是从JavaScript AJAX调用的。当然,MyModel.JSONRequest
的定义正好反映了JSON对象。问题是-迭代层次结构的方法不起作用。它没有进入子类。这很奇怪,因为相同的代码在我的测试WinForms应用程序中工作
编辑:
以下是JSONRequest的类定义
public class SubObjectClass
{
public string UID {get; set;}
public int Number {get; set;}
public string Text {get; set;}
}
public class JSONRequest
{
public class RequestClass
{
public int number { get; set; }
public SubObjectClass SubObject {get; set}
}
public class RequestHeader
{
public int version { get; set; }
public string token {get; set}
}
public RequestHeader header { get; set; }
public RequestClass request { get; set; }
}
任何帮助、评论、提示、想法……都将不胜感激。多谢各位
解决了的
如果有人会有类似的问题。有两件事:
1.当递归遍历类层次结构时,我必须使用此行检查属性是否是类的实例,我是否应该介入(因为String也是一个我不想介入的类)
2.我的解决方案由2个项目组成。“GetProperties”位于第一个项目(WCF服务项目)中引用的第二个项目中。当我将“GetReflection”代码直接放在WCF项目中时,它就工作了。我知道你写过,“当然,
MyModel.JSONRequest
的定义正好反映了JSON对象”,但是你能给我们展示一下吗?@dbc谢谢。明天我不在办公室的时候我会这样做。模型类由[DataContract]修饰,其成员由[DataMember]修饰。@dbc我更新了问题:)我刚刚尝试了你的代码GetProperties(instanceOfClassA)
——它没有打印任何内容。这是因为您只打印空属性的信息,在这种情况下,只打印名称。对于JSON类,GetProperties()
抛出一个异常,因为您需要跳过索引属性,即那些property.GetIndexParameters().Length>0
-例如。@dbc感谢这一点。奇怪的是,它可以在WinForms应用程序上运行。然而,我注意到在“GetProperties”方法中的WCF服务中,“classType”不包含任何属性。当我执行“classType.ToString()”时,它只是说“System.Object”,我想它不包含类类型信息……和“foreach(PropertyInfo property in classType.GetProperties(BindingFlags…)包含0个元素。它不起任何作用。不确定捕获点在哪里。无论如何,请随意发表您的评论作为答案,我将为您提供至少一点时间和良好的提示。谢谢dbc