2015-05-21 29 views
-1

出于某种原因,用下面的代码:
[编辑]更新后的代码按建议...仍然给我同样的错误。列表<MyObject>未能序列化

[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.' 

这究竟是为什么?我真的不明白这一点。请帮忙。

回答

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; } 
    } 

您应该删除属性

+0

做到了,并没有解决问题... – MaxOvrdrv

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(); 
    } 
+1

看到我的“自己的答案”下面......有一个与真的没有问题代码开始... VisualStudio IDE有问题。在我重新启动它之后,所有东西都会编译原来的样子......也就是说,即使没有一个需要的[[Serializable]属性,它应该放在第一位......但是,谢谢:) – MaxOvrdrv