【发布时间】:2013-03-16 05:12:32
【问题描述】:
我有 2 节课:
public class testClass1
{
public string Name { get; set; }
public testClass2 testClass2Object { get; set; }
}
public class testClass2
{
public testClass2() { }
public testClass2(int i) { TestProperty = i; }
public int TestProperty { get; set; }
}
我想用webMethod 返回头等舱的对象:
[WebMethod]
public testClass1 testMethod()
{
testClass1 test = new testClass1();
test.Name = "stackoverflow";
test.testClass2Object = new testClass2(2);
return test;
}
但我没有从 testClass1 对象中获取 testClass2 属性的值。
我尝试了[Serializable]
[XmlInclude(typeof(testClass2))] 注释,但没有任何改变。有什么建议?
【问题讨论】:
-
如果删除
public testClass2(int i)构造函数会发生什么?如果您将声明更改为public testClass2(int testProperty )会怎样? (我问第二个是因为我知道 ctor 参数的实际名称会影响 JSON 序列化;但不确定它是否会影响其他序列化)。 -
@Matthew Watson 删除了构造函数,现在可以使用了,谢谢。
-
@Matthew Watson 如果我想发送它的对象,我不能有带参数的构造函数?
标签: .net web-services serialization