J'ai un stumper élémentaire en utilisant sérialisation dans une application ASP.NET 2.0 sur une classe C# contenant une propriété ENUM. Je crois comprendre que la sérialisation des énumérations est prise en charge si elles sont mappées sur des entiers. Donc, je n'arrive pas à comprendre pourquoi j'ai ce problème sérialiser/désérialiser mon énumération.Problème sérialisation Enum dans .NET
Mon code:
[Serializable]
public class Report
{
public PercentTime paramPercentRange;
// Constructors
public Report()
{
}
public Report(PercentTime percentRange)
{
paramPercentRange = percentRange;
}
}
public enum PercentTime
{
Null = 0,
ZeroToFivePercent = 1,
FiveToTenPercent = 2,
TenToFifteenPercent = 3,
FifteenToTwentyPercent = 4,
MoreThanTwentyPercent = 5
}
// Serialize Report to a HiddenField
public void SaveReportObject(Report reportObj, HiddenField hiddenReportObj)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
// Seralize Report Object to Binary Format
formatter.Serialize(stream, reportObj);
stream.Position = 0;
// Convert Stream to ASCII Encoding
StreamReader reader = new StreamReader(stream, Encoding.ASCII);
// Store Report Object as a Base64 Encoded String in a HiddenField
hiddenReportObj.Value = Convert.ToBase64String(Encoding.ASCII.GetBytes(reader.ReadToEnd()));
// Close Streams
reader.Close();
stream.Close();
}
public Report GetReportObject(String strBase64)
{
Report report;
Stream stream = new MemoryStream();
IFormatter formatter = new BinaryFormatter();
StreamWriter writer = new StreamWriter(stream, Encoding.ASCII);
writer.AutoFlush = true;
stream.Position = 0;
// Convert Base64 String to ASCII encoded Stream
writer.Write(Encoding.ASCII.GetString(Convert.FromBase64String(strBase64)));
stream.Position = 0;
// Deserialze ASCII encoded Stream
report = (Report)formatter.Deserialize(stream); // error
// Close Streams
writer.Close();
stream.Close();
return report;
}
Je reçois le texte suivant Invalid BinaryFormatter Erreur:
at System.Runtime.Serialization.Formatters.Binary.SizedArray.IncreaseCapacity(Int32 index)
at System.Runtime.Serialization.Formatters.Binary.SizedArray.set_Item(Int32 index, Object value)
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
at Pages_Reports_PercentTimeLobbyingReport.GetReportObject(String strBase64)
Quand je regarde l'objet de flux, sa longueur est de 287, mais la position est 282 Ma première supposition est que c'est un problème de ne pas lire le flux entier, mais je suis capable de changer la classe Report pour stocker l'enum comme un entier et cela fonctionne bien. Je suis très intéressé à apprendre quel est le problème ici.
Merci pour la trace de la pile, mais quel était le _exception_? Postez le résultat de 'ex.ToString()'. –