Reputation: 3783
I am trying to serialize an enum object within a class. Currently I am going to be using a binary reader and writer to serialize/de-serialize.
My enum is called SelectionType and has a few possible values.
In my serialize method I write the toString value of the enum object I want to save.
I then dont know what to do with the string to turn this back into an enum when I read the string. I dont really want to have a hard coded case for each possible value.
I basically want to say; SelectionType newenumobject = SelectionType.String // where string is the value read from the file.
ANy help would be greatly appreciated:
here is what I have:
public override void Serialize(Stream stream)
{
System.Diagnostics.Debug.WriteLine("in level select serialize");
using (BinaryWriter writer = new BinaryWriter(stream))
{
//Debug.WriteLine("the type is" + SelectionType.ToString());
writer.Write(SelectionType.ToString());
// Write out the full name of all the types in our stack so we can
// recreate them if needed.
//byte[] bytes = new byte[SelectionType.ToString().Length * sizeof(char)];
//System.BitConverter.GetBytes(mSlectionType);
//writer.Write(bytes, 0, bytes.Length);
}
}
public override void Deserialize(Stream stream)
{
using (BinaryReader reader = new BinaryReader(stream))
{
while (reader.BaseStream.Position < reader.BaseStream.Length)
{
// read a line from our file
string line = reader.ReadString();
// if it isn't blank, we can create a screen from it
if (!string.IsNullOrEmpty(line))
{
Debug.WriteLine("line is" + line);
Type screenType = Type.GetType(line);
//SelectionType selection = Enum.Parse(screenType, line, false);
}
}
// char[] chars = new char[bytes.Length / sizeof(char)];
//System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
//new string(chars);
}
Upvotes: 1
Views: 5303
Reputation: 473
If you care about file size and read/write speed, you should save the underlying value of the enumeration rather than converting it to a string.
// be sure to use the correct backing type for your enum (default is 32-bit int)
writer.Write((int)SelectionType);
//...
selection = (SelectionType)reader.ReadInt32()
Upvotes: 3
Reputation: 326
You can just use Enum.Parse. Here is an example: http://dotnetfiddle.net/0e1Ika
var myValue = MyEnum.Value3;
var stringRepresentation = myValue.ToString();
var intRepresentation = (int)myValue;
Console.WriteLine("String-Value: \"{0}\"", stringRepresentation);
Console.WriteLine("Int-Value: {0}", intRepresentation);
var parsedFromString = (MyEnum)Enum.Parse(typeof(MyEnum), stringRepresentation);
var parsedFromInt = (MyEnum)Enum.Parse(typeof(MyEnum), intRepresentation.ToString());
Console.WriteLine("Parsed from string: {0}", parsedFromString);
Console.WriteLine("Parsed from int: {0}", parsedFromInt);
Hope that helps.
Best regards, Chris
ps: uh, seems @dave-bish was faster :)
Upvotes: 2
Reputation: 19646
if you know the type of the enum, you can use Enum.Parse
var myValue = (EnumType)Enum.Parse(typeof(EnumType), "StringValue");
[Edit] I see you have it commented out in your example - Why didn't this work for you?
Upvotes: 0