列表<myobject>无法序列化

时间:2015-05-21 14:59:30

标签: c#

出于某种原因,使用以下代码:
[编辑] 根据建议更新了代码......仍然给我同样的错误。

[Serializable]
public class WebSiteSettings
{
    public string applicationPool { get; set; }
    public List<SiteBinding> bindings { get; set; }
    public int id { get; set; }
    public string name { get; set; }
    public string physicalPath { get; set; }
    public SiteCredential credentials { get; set; }
    public LogonType logonMethod { get { return this.credentials.type; } set { this.credentials.type = value; } }
    public bool autoStart { get; set; }
    public bool limits { get; set; }
    public int? connectionTimeout { get; set; }
    public uint? maxBandwidth { get; set; }
    public uint? maxConnections { get; set; }
    public string enabledProtocols { get; set; }
    public bool traceFailedRequestsLogging { get; set; }
    public string directory { get; set; }
    public bool enabled { get; set; }
    public int? maxLogFiles { get; set; }
}

[Serializable]
public enum BindingType
{
    http,
    https,
    net_tcp,
    net_pipe,
    net_msmq,
    msmq_formatname
};

[Serializable]
public class SiteBinding
{
    public BindingType type { get; set; }
    public string hostName { get; set; }
    public int? port { get; set; }
    public BindingIP ip { get; set; }
    public string sslCertificate { get; set; }
    public string bindingInfo { get; set; }

    public SiteBinding() { }

    public override string ToString()
    {
        return this.type.ToString() + ((this.type == BindingType.http || this.type == BindingType.https) ? ":" + this.ip.ToString() + ":" + this.hostName : ":" + this.bindingInfo);
    }
}

[Serializable]
public class BindingIP
{
    public bool Unassigned { get; set; }
    private int _Eight { get; set; }
    private int _Sixteen { get; set; }
    private int _TwentyFour { get; set; }
    private int _ThirtyTwo { get; set; }
    private int _Port { get; set; }
    public int Eight { get { return this._Eight; } set { if (value >= 0 && value <= 223) { this._Eight = value; } else { throw new Exception("Invalid first bit address. Must be between 0 and 223."); } } }
    public int Sixteen { get { return this._Sixteen; } set { if (value >= 0 && value <= 255) { this._Sixteen = value; } else { throw new Exception("Invalid second bit address. Must be between 0 and 255."); } } }
    public int TwentyFour { get { return this._TwentyFour; } set { if (value >= 0 && value <= 255) { this._TwentyFour = value; } else { throw new Exception("Invalid third bit address. Must be between 0 and 255."); } } }
    public int ThirtyTwo { get { return this._ThirtyTwo; } set { if (value >= 0 && value <= 255) { this._ThirtyTwo = value; } else { throw new Exception("Invalid fourth bit address. Must be between 0 and 255."); } } }
    public int Port { get { return this._Port; } set { if (value >= 0 && value <= 65535) { this._Port = value; } else { throw new Exception("Invalid port address. Must be between 0 and 65535."); } } }

    public BindingIP() { }

    public BindingIP(string ip)
    {
        if (ip.StartsWith("All Unassigned"))
        {
            this.Unassigned = true;
            if (ip.Contains(":"))
            {
                string port = ip.Split(new string[] { ":" }, StringSplitOptions.None)[1];
                int p;

                if (!int.TryParse(port, out p))
                    throw new ArgumentException("Cannot convert string to valid IP address... port is not a number.");

                try
                {
                    this.Port = p;
                }
                catch (Exception ex)
                {
                    throw new ArgumentException("Cannot convert string to valid IP address... See inner exception for details.", ex);
                }
            }
        }
        else
        {
            List<string> pieces = ip.Split(new string[] { "." }, StringSplitOptions.None).ToList();
            if (pieces.Count != 4)
                throw new ArgumentException("Cannot convert string to valid IP address... invalid count of bits.");

            if (!pieces[3].Contains(":"))
                throw new ArgumentException("Cannot convert string to valid IP address... missing port.");

            string port = pieces[3].Split(new string[] { ":" }, StringSplitOptions.None)[1];
            pieces[3] = pieces[3].Split(new string[] { ":" }, StringSplitOptions.None)[0];

            int a;
            int b;
            int c;
            int d;
            int p;


            if (!int.TryParse(pieces[0], out a))
                throw new ArgumentException("Cannot convert string to valid IP address... first set of eight bits is not a number.");

            if (!int.TryParse(pieces[1], out b))
                throw new ArgumentException("Cannot convert string to valid IP address... second set of eight bits is not a number.");

            if (!int.TryParse(pieces[2], out c))
                throw new ArgumentException("Cannot convert string to valid IP address... third set of eight bits is not a number.");

            if (!int.TryParse(pieces[3], out d))
                throw new ArgumentException("Cannot convert string to valid IP address... fourth set of eight bits is not a number.");

            if (!int.TryParse(port, out p))
                throw new ArgumentException("Cannot convert string to valid IP address... port is not a number.");

            try
            {
                this.Eight = a;
                this.Sixteen = b;
                this.TwentyFour = c;
                this.ThirtyTwo = d;
                this.Port = p;
            }
            catch (Exception ex)
            {
                throw new ArgumentException("Cannot convert string to valid IP address... See inner exception for details.", ex);
            }

        }
    }

    public string ToLongString()
    {
        return (this.Unassigned ? "*" : this.Eight.ToString() + "." + this.Sixteen.ToString() + "." + this.TwentyFour.ToString() + "." + this.ThirtyTwo.ToString()) + (this.Port == 0 ? "" : ":" + this.Port.ToString());
    }

    public override string ToString()
    {
        return this.Unassigned ? "" : this.Eight.ToString() + "." + this.Sixteen.ToString() + "." + this.TwentyFour.ToString() + "." + this.ThirtyTwo.ToString();

    }
}

[Serializable]
public enum LogonType
{
    Interactive,
    Batch,
    Network,
    ClearText
}

[Serializable]
public class SiteCredential
{
    public string username { get; set; }
    public string password { get; set; }
    public LogonType type { get; set; }

    public SiteCredential() { }
}

我尝试构建时遇到此错误:

Code generation for property 'bindings' failed. Error was: 'Type 
'MyLibrary.SiteBinding' in Assembly 'MyLibrary, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null' is not marked as serializable.'

为什么会这样?我真的不明白这一点。请帮忙。

3 个答案:

答案 0 :(得分:0)

使用此代码,您将收到编译时错误,因为您无法将[Serializable]属性应用于属性:

    [Serializable]
    public class SiteBinding
    {
        public BindingType type { get; set; }
        public string hostName { get; set; }
        public int? port { get; set; }
        [Serializable] //remove this line
        public BindingIP ip { get; set; }
        public string sslCertificate { get; set; }
    }

您应该删除属性

答案 1 :(得分:0)

答案如下,在此处找到:

http://www.codeproject.com/Questions/128351/Code-generation-for-property-yyy-failed

我所要做的就是重新启动Visual Studio,然后重新构建所有内容。

答案 2 :(得分:0)

您忘记在Serialize()调用中添加所有必需类型?

此代码适用于所有课程:

    static void Main(string[] args)
    {

        SiteBinding s = new SiteBinding();
        s.ip = new BindingIP("127.0.0.1:8080");
        s.type = new BindingType();
        WebSiteSettings sets = new WebSiteSettings();
        sets.credentials = new SiteCredential();
        sets.bindings = new List<SiteBinding>() {s};
        XmlSerializer ser = new XmlSerializer(sets.GetType());
        ser.Serialize(new System.IO.MemoryStream(),sets);

        Console.Read();
    }