开发者

Serialize error from user control

I have created a user control (CheckedDirTree) that exposes a CheckedFolder property which in turn returns an IEnumerable of the FullPath 开发者_JAVA百科property of Nodes checked in the control. Something like this:

 public IEnumerable<string> CheckedFolders
    {
        get
        {
            foreach (TreeNode node in treeView1.Nodes[0].DescendantNodes())
            {
                if(node.Checked && !node.FullPath.Equals(_directoryRoot))
                    yield return node.FullPath;
            }
        }
    }

This is being fed to another class's (SymbolsShareDto) Folders property after this cntrol is shown in a grid and the user has checked some folders:

using (var dirControl = new CheckedDirForm(symbolsShare))
                {
                    if (dirControl.ShowDialog() == DialogResult.OK)
                    {
                        var symbolsShareObj = bindingSourceShare.Current as SymbolShareModel;
                        if (symbolsShareObj != null) symbolsShareObj.Folders = dirControl.CheckedFolders;
                    }
                }

[DataContract]
public class SymbolShareDTO
{
    public SymbolShareDTO(){}

    [DataMember]
    public string Share { get; set; }
    [DataMember]
    public string BackupTo { get; set; }
    [DataMember]
    public IEnumerable<string> Folders { get; set; }

    public override string ToString()
    {
        return string.Format("Share: {0}{1}BackupTo: {2}{3}Folders: {4}", Share, Environment.NewLine, BackupTo,
                             Environment.NewLine, Folders.Count());
    }
}

However when I serialize the SymbolsShareDto, I'm getting an error saying

CheckedDirTree+<get_CheckedFolders>d__6' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. 

Any thoughts ? Do I need to return a new List from CheckedFolders property instead.

thanks Sunit


Do CheckedFolders.ToArray() and serialize that.


use [DataContract] for your class (msdn)

and [DataMember] for each property (msdn)


You're probably trying to send instances of SymbolsShareDto through WCF.

All you have to do is to mark it with DataContractAttribute and all the property with DataMember :

[DataContract]
public class SymbolsShareDto 
{
     [DataMember]
     public int Data1 { get; set;};

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

I hope this help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜