Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何序列化在wcf中只有一次复杂属性的对象?_C#_.net_Wcf - Fatal编程技术网

C# 如何序列化在wcf中只有一次复杂属性的对象?

C# 如何序列化在wcf中只有一次复杂属性的对象?,c#,.net,wcf,C#,.net,Wcf,我想序列化一个名为ComplexModel的对象,该对象在wcf中具有复杂属性,首先我使用List SimpleModel作为对象的属性 我的wcf型号 [DataContract] //[KnownType(typeof(SimpleModel[]))] public class ComplexModel { [DataMember] public String Name { get; set; } [DataMember] public Int32 Ag

我想序列化一个名为
ComplexModel
的对象,该对象在wcf中具有复杂属性,首先我使用
List SimpleModel
作为对象的属性

我的wcf型号

  [DataContract]
//[KnownType(typeof(SimpleModel[]))]
public class ComplexModel
{
    [DataMember]
    public String Name { get; set; }

    [DataMember]
    public Int32 Age { get; set; }

    //change List<SimpleModel> to SimpleModelCollection
    [DataMember]
    public SimpleModelCollection SimpleModel { get; set; }
}

/// <summary>
///  defined a custom type to implement the IEnumerable interface instead of List SimpleModel
/// </summary>
[Serializable]
[CollectionDataContract]
[KnownType(typeof (SimpleModel))]
public class SimpleModelCollection : IEnumerable<SimpleModel>
{
    public IList<SimpleModel> List = new List<SimpleModel>();

    public SimpleModelCollection()
    {
    }

    public SimpleModelCollection(IList<SimpleModel> objs)
    {
        List = objs;
    }

    public void Add(SimpleModel obj)
    {
        List.Add(obj);
    }

    //IEnumerable<SimpleModel> member
    public IEnumerator<SimpleModel> GetEnumerator()
    {
        return List.GetEnumerator();
    }

    //IEnumerable member
    IEnumerator IEnumerable.GetEnumerator()
    {
        return List.GetEnumerator();
    }
}

[DataContract]
public class SimpleModel
{
    [DataMember]
    public string MumName { get; set; }

    [DataMember]
    public int Point { get; set; }

    [DataMember]
    public DateTime BirthDate { get; set; }
}
我的wcf课程

public class TestService1 : ITestService1
{
    public ComplexModel DoWork()
    {
        var SimpleModels = new SimpleModelCollection();
        SimpleModels.Add(new SimpleModel
        {
            MumName = "def",
            BirthDate = DateTime.MinValue,
            Point = 99
        });
        SimpleModels.Add(new SimpleModel
        {
            MumName = "hig",
            BirthDate = DateTime.Now,
            Point = 100
        });
        return new ComplexModel
        {
            Age = 10,
            Name = "abc",
            SimpleModel = SimpleModels
        };
    }
}
public class TestService1 : ITestService1
{
    public ComplexModel DoWork()
    {
        var SimpleModels = new SimpleModelCollection();
        SimpleModels.Add(new SimpleModel
        {
            MumName = "def",
            BirthDate = DateTime.MinValue,
            Point = 99
        });
        SimpleModels.Add(new SimpleModel
        {
            MumName = "hig",
            BirthDate = DateTime.Now,
            Point = 100
        });
        return new ComplexModel
        {
            Age = 10,
            Name = "abc",
            SimpleModel = SimpleModels
        };
    }
}