C#中的TypeInitialization异常

时间:2017-06-05 00:33:43

标签: c#

我基于本地.csv文件的信息,实现了有限状态机的简单实现:

TransitionTable.csv就像下面这样简单:

q_src,eve,q_dst
q0,sig1,q1
q1,sig2,q2

这是我的主要课程:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BacktrackingPathFinder
{
    class Program
    {
        static IEnumerable<string[]> transitions = File.ReadAllLines(@"TransitionTable.csv").Skip(1).Select(x => x.Split(','));
        static void Main(string[] args)
        {
            State q0 = new State("q0");
            Event e1 = new Event("sig1"); 
            PathFinder pf = new PathFinder();
            State s = pf.FindDestination(q0, e1, transitions);
            Console.WriteLine(s.ID);
            Console.ReadLine();
        }
    }
}

PathFinder.cs定义如下:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BacktrackingPathFinder
{
    public class PathFinder
    {
        public PathFinder()
        {

        }

        public State FindDestination(State q0, Event s0, IEnumerable<string[]> transitions)
        {
            var newq = from t in transitions
                       where t[0] == q0.ID && t[1] == s0.ID
                       select t[2];
            return new State(newq.FirstOrDefault().ToString());
        }
    }
}

根据我的.csv文件,输出应为q1,但会抛出以下异常,我无法弄清楚:

  

System.TypeInitializationException未处理消息:未处理   类型&#39; System.TypeInitializationException&#39;的异常发生在   mscorlib.dll附加信息:类型初始化程序   &#39; BacktrackingPathFinder.Program&#39;抛出异常。

更新

我做了@Poke说:

class Program
    {    
        static void Main(string[] args)
        {
            IEnumerable<string[]> transitions = File.ReadAllLines(@"TransitionTable.csv").Skip(1).Select(x => x.Split(','));
            State q0 = new State("q0");
            Event e1 = new Event("sig1"); 
            PathFinder pf = new PathFinder();
            State s = pf.FindDestination(q0, e1, transitions);
            Console.WriteLine(s.ID);
            Console.ReadLine();
        }
    }

未解决异常,并抛出新的FileNotFoundException.csv文件位于项目目录中: enter image description here

0 个答案:

没有答案