使用Web服务传递具有复杂属性的对象

pep*_*ela 5 .net serialization web-services

我有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; }
    }
Run Code Online (Sandbox Code Playgroud)

我想用以下方法返回第一类的对象webMethod:

    [WebMethod]
    public testClass1 testMethod()
    {
        testClass1 test = new testClass1();
        test.Name = "stackoverflow";
        test.testClass2Object = new testClass2(2);
        return test;
    }
Run Code Online (Sandbox Code Playgroud)

但我没有testClass2testClass1对象获得属性的值.

我试过[Serializable] [XmlInclude(typeof(testClass2))]注释但没有改变.有什么建议?

Rob*_*ton 1

如果我“按原样”运行代码并调用 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>
Run Code Online (Sandbox Code Playgroud)

你期待一些不同的东西吗?也许我错过了一些东西。

如果这是一个更大项目的一部分,也许可以尝试将此代码放入一个新项目中,看看它是否可能是设置或其他配置类型的问题。