我有两门课:
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
属性的值。
我尝试了[可序列化]
[XmlIninclude(typeof(testClass2))]
注释但没有任何改变。有什么建议么?
最佳答案
如果我“按原样”运行代码并调用 testMethod(),我会得到...
<testClass1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<Name>stackoverflow</Name>
<testClass2Object>
<TestProperty>2</TestProperty>
</testClass2Object>
</testClass1>
你期待一些不同的东西吗?也许我错过了一些东西。
如果这是一个更大项目的一部分,也许可以尝试将此代码放入一个新项目中,看看它是否可能是设置或其他配置类型的问题。
关于.net - 通过 Web 服务传递具有复杂属性的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15636538/