我有以下简单的XML可序列化类型:
[XmlType]
public class TestType
{
public System.Net.SecurityProtocolType ProtocolType { get; set; }
}
var instanceToSerialize = new TestType { ProtocolType = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 };
我可以在我的计算机和几台计算机上很好地序列化这个对象的实例.但是在我测试的其中一台计算机上,我遇到以下异常:
System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: Instance validation error: '4080' is not a valid value for System.Net.SecurityProtocolType. at System.Xml.Serialization.XmlCustomFormatter.FromEnum(Int64 val, String[] vals, Int64[] ids, String typeName) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterTestType.Write1_SecurityProtocolType(SecurityProtocolType v) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterTestType.Write3_TestType(String n, String ns, TestType o, Boolean isNullable, Boolean needType) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterTestType.Write4_TestType(Object o)
我已经比较了sgen.exe
工作和失败案例的生成代码.在工作案例中,我生成了以下代码:
string Write1_SecurityProtocolType(global::System.Net.SecurityProtocolType v) {
string s = null;
switch (v) {
case global::System.Net.SecurityProtocolType.@Ssl3: s = @"Ssl3"; break;
case global::System.Net.SecurityProtocolType.@Tls: s = @"Tls"; break;
case global::System.Net.SecurityProtocolType.@Tls11: s = @"Tls11"; break;
case global::System.Net.SecurityProtocolType.@Tls12: s = @"Tls12"; break;
default: s = FromEnum(((System.Int64)v), new string[] {@"Ssl3",
@"Tls",
@"Tls11",
@"Tls12"}, new System.Int64[] {(long)global::System.Net.SecurityProtocolType.@Ssl3,
(long)global::System.Net.SecurityProtocolType.@Tls,
(long)global::System.Net.SecurityProtocolType.@Tls11,
(long)global::System.Net.SecurityProtocolType.@Tls12}, @"System.Net.SecurityProtocolType"); break;
}
return s;
}
而在失败的代码上,这是生成的代码:
string Write1_SecurityProtocolType(global::System.Net.SecurityProtocolType v) {
string s = null;
switch (v) {
case global::System.Net.SecurityProtocolType.@Tls: s = @"Tls"; break;
case global::System.Net.SecurityProtocolType.@Tls11: s = @"Tls11"; break;
case global::System.Net.SecurityProtocolType.@Tls12: s = @"Tls12"; break;
default: s = FromEnum(((System.Int64)v), new string[] {@"Tls",
@"Tls11",
@"Tls12"}, new System.Int64[] {(long)global::System.Net.SecurityProtocolType.@Tls,
(long)global::System.Net.SecurityProtocolType.@Tls11,
(long)global::System.Net.SecurityProtocolType.@Tls12}, @"System.Net.SecurityProtocolType"); break;
}
return s;
}
您可以注意到,Ssl3
在失败的情况下不会生成枚举成员的代码.你知道为什么代码丢失了吗?