使用数据库类型的列表

时间:2011-06-08 15:41:16

标签: wcf list observablecollection datamember

我是wcf的新手,我创建了一个像这样的DataContract:

    [DataContract]
    public class directoriesUser
    {
        [DataMember]
        public string NameDir { get; set; }

        [DataMember]
        public directoriesUser Parent { get; set; }


        private ObservableCollection<directoriesUser> _userDirs = new ObservableCollection<directoriesUser>();

        [DataMember]
        public ObservableCollection<directoriesUser> UserDirectories
        {
            get { return _userDirs; }
            set { _userDirs = value; }
        }
    }

但是当我像上面这样使用这个类时,该服务抛出异常。有人能帮助我吗?

    public directoriesUser getHomeUser(int user_id)
    {
        directoriesUser dirhome = new directoriesUser();
        // this doesn't works
        dirhome.UserDirectories = new ObservableCollection<directoriesUser>{
            new directoriesUser { NameDir = "dir1", Parent = dirhome},
            new directoriesUser {NameDir = "dir2", Parent = dirhome}
        };
        // neither this one
        dirhome.UserDirectories.Add(new directoriesUser {NameDir = "dir3", Parent = dirhome})
        return dirhome;
    }

1 个答案:

答案 0 :(得分:0)

当wcf序列化实例时,您的directoriesUser类可能会导致递归。这基本上是由指向层次结构的Parent属性引起的。 这是关于此的more detailed article