将字符串从二进制文件转换为枚举

时间:2016-01-04 22:25:49

标签: c# enums record

每当我尝试从二进制文件中读取字符串值时,我都会收到以下错误(其中字符串值为枚举类型)

  

未处理的类型' System.ArgumentException'发生在   mscorlib.dll中

     

其他信息:要求的价值' Bulldog'没找到。

请注意,我的第一条记录显示正常并显示在控制台窗口中,但我的第二条记录(标题为“Bulldog'”)导致了我认为的问题。

如您所见,TGenreTypes声明如下。

enum TGenreTypes
{
    Romance,
    SciFi,
    Action,
    Thriller,
    Horror,
    Adventure
};

struct TBook
{ 
    public string author;
    public string title;
    public TGenreTypes genre;
    public int bookid;
};

然后,在阅读声明中,我已经使用了Enum.Parse,如下所示:

 do
 {
     //Now write variable contents to file
     myBooks.title = readerFromFile.ReadString();
     myBooks.author = readerFromFile.ReadString();
     myBooks.genre = (TGenreTypes) Enum.Parse(typeof(TGenreTypes),readerFromFile.ReadString());
     myBooks.bookid = readerFromFile.ReadInt16();
     Console.WriteLine("Title: {0}", myBooks.title);
     Console.WriteLine("Author: {0}", myBooks.author);
     Console.WriteLine("Genre: {0}", myBooks.genre);
     Console.WriteLine("BookID: {0}", myBooks.bookid);
 }
 while (currentFile.Position <= currentFile.Length);

但是,当它尝试从文件中读取第二条记录时,我收到上述错误消息。很明显,第二张唱片的标题是“Bulldog”......这很奇怪,因为它在“#fayre”类型中解析了#39;线。

这是完整的程序,它仍然给我错误:

namespace Reading_from_Binary_File
{
    class Program
    {
        enum TGenreTypes
        { 
            None = 0,
            Romance,
            SciFi,
            Action,
            Thriller,
            Horror,
            Adventure,
            Fantasy
        };

        struct TBook
        {
            public string author;
            public string title;
            public TGenreTypes genre;
            public int bookid;
        };

        static void Main(string[] args)
        {
            FileStream currentFile;
            BinaryReader readerFromFile;

            currentFile = new FileStream("Test.bin", FileMode.Open);
            readerFromFile = new BinaryReader(currentFile);

            TBook myBooks;

            do
            {
                //Now write variable contents to file
                myBooks.title = readerFromFile.ReadString();
                myBooks.author = readerFromFile.ReadString();
                myBooks.genre = (TGenreTypes)Enum.Parse(typeof(TGenreTypes),readerFromFile.ReadString());
                myBooks.bookid = readerFromFile.ReadInt16();
                Console.WriteLine("Title: {0}", myBooks.title);
                Console.WriteLine("Author: {0}", myBooks.author);
                Console.WriteLine("Genre: {0}", myBooks.genre);
                Console.WriteLine("BookID: {0}", myBooks.bookid);
            }
            while (currentFile.Position < currentFile.Length);

            //close the streams
            readerFromFile.Close();
            currentFile.Close();

            Console.ReadLine();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您可以尝试使用Enum.TryParse方法而不是Parse。通过这种方式,您可以避免使用错误值的异常:

TGenreTypes genre;

if (Enum.TryParse(readerFromFile.ReadString(), out genre))
{
    myBooks.genre = genre;
}
else
{
    // Handle when parse error if needed
}

我建议你添加一个&#34;无&#34;输入零值到枚举。这是一个很好的做法,您可以将其作为记录的默认值,并将错误转换为枚举。有关详细信息,请查看此link

enum TGenreTypes
{
    None = 0,
    Romance,
    SciFi,
    Action,
    Thriller,
    Horror,
    Andventure
};

答案 1 :(得分:0)

似乎与您的相关问题Reading from binary file error中的问题相同。 为了再次为别人解开谜团的答案:

尝试交换

myBooks.bookid = readerFromFile.ReadInt16();

myBooks.bookid = readerFromFile.ReadInt32();

默认情况下,intSystem.Int32的别名。

在你的结构中

struct TBook
{
    public string author;
    public string title;
    public string genre; //TGenreTypes genre;
    public int bookid;
};

您已指定int bookid,然后是System.Int32。 因此,只读取2 bytes而不是4 bytes会导致流中有2 bytes。因此循环不会中断。在循环中,您将尝试阅读另一个&#34; set&#34;没有的数据(只有2个字节)。