C# 列表<;MyObject>;无法序列化
出于某种原因,使用以下代码:C# 列表<;MyObject>;无法序列化,c#,C#,出于某种原因,使用以下代码: [编辑]根据建议更新代码。。。还是给我同样的错误 [Serializable] public class WebSiteSettings { public string applicationPool { get; set; } public List<SiteBinding> bindings { get; set; } public int id { get; set; } public string name { g
[编辑]根据建议更新代码。。。还是给我同样的错误
[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() { }
}
[可序列化]
公共类网站设置
{
公共字符串应用程序池{get;set;}
公共列表绑定{get;set;}
公共int id{get;set;}
公共字符串名称{get;set;}
公共字符串物理路径{get;set;}
公共站点凭据凭据{get;set;}
public LogonType logonMethod{get{返回this.credentials.type;}set{this.credentials.type=value;}}
公共bool自动启动{get;set;}
公共布尔极限{get;set;}
public int?connectionTimeout{get;set;}
公共uint?最大带宽{get;set;}
公共uint?maxConnections{get;set;}
公共字符串enabledProtocols{get;set;}
公共bool traceFailedRequestsLogging{get;set;}
公共字符串目录{get;set;}
已启用公共bool的{get;set;}
公共int?maxLogFiles{get;set;}
}
[可序列化]
公共枚举绑定类型
{
http,,
https,
net_tcp,
净管,
net_msmq,
msmq_格式名称
};
[可序列化]
公共类站点绑定
{
公共绑定类型{get;set;}
公共字符串主机名{get;set;}
公共int?端口{get;set;}
公共绑定ip{get;set;}
公共字符串sslCertificate{get;set;}
公共字符串bindingInfo{get;set;}
公共站点绑定(){}
公共重写字符串ToString()
{
返回this.type.ToString()+((this.type==BindingType.http | | this.type==BindingType.https)?“:“+this.ip.ToString()+”:“+this.hostName:”+this.bindingInfo);
}
}
[可序列化]
公共类绑定IP
{
公共布尔未分配{get;set;}
私有整数{get;set;}
私有整数{get;set;}
私有整数_二十四{get;set;}
私有整数_ThirtyTwo{get;set;}
私有int_端口{get;set;}
public int-Eight{get{返回此值。Eight;}set{if(value>=0&&value=0&&value=0&&value=0&&value=0&&value使用此代码,您将出现编译时错误,因为无法将[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; }
}
您应该删除属性答案如下所示:
我所要做的就是重新启动Visual Studio,然后重新构建所有内容。您忘记在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();
}
static void Main(字符串[]args)
{
SiteBinding s=新的SiteBinding();
s、 ip=新绑定ip(“127.0.0.1:8080”);
s、 type=新的BindingType();
WebSiteSettings集合=新的WebSiteSettings();
set.credentials=新站点凭据();
set.bindings=new List(){s};
XmlSerializer ser=新的XmlSerializer(sets.GetType());
序列化(新的System.IO.MemoryStream(),集合);
Console.Read();
}
请参见下面我的“自己的答案”…代码一开始就没有问题…VisualStudio IDE出现了问题。在我重新启动它之后,一切都按照原来的方式编译…也就是说,甚至不需要一个[Serializable]
属性,因为它本来就应该是这样的…但是谢谢:)
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();
}