c# - System.TypeInitializationException'错误

时间:2017-12-08 08:36:05

标签: c# visual-studio

我在c#visual studio中有一个程序,当我尝试运行它时会抛出一个错误:

  

抛出异常:SagemReader.dll中的'System.TypeInitializationException'

     

附加信息:'SagemReader.SagemReaderAPI'的类型初始化程序引发了异常。

我尝试在这个帖子中寻找解决方案,但解决方案对我不起作用,所以我不得不发布一个新的解决方案。 这是我的代码中引发错误的部分:

try
{
    readers = new char[-1 + 1][];

    //initailize Smart Card Reader
    Debug.WriteLine("oblSagem");
    oblSagem = SagemReaderAPI.InitSagemReader();
    //oblSagem.Initialize_Reader();
    //Test KC 20140922
    if (!booSagemReaderBusy)
    {
        booSagemReaderBusy = true;
        booCurrentSagemReaderStatusOK = oblSagem.getReaders(ref readers);
    }

    if (!booCurrentSagemReaderStatusOK)
    {
        booSagemReaderBusy = false;
        booPreviousSagemReaderStatusOK = booCurrentSagemReaderStatusOK;
    }
    else
        booSagemReaderBusy = false;

    Debug.WriteLine($"Sagem Busy : {booSagemReaderBusy} ; Current Sagem Status : {booCurrentSagemReaderStatusOK} ; Previous Sagem Status : {booPreviousSagemReaderStatusOK}" );
}
catch (Exception ex)
{
    CommonMethods.LogError("MemberInfo()", "[SEGAM ERROR]" + ex);
    booCurrentSagemReaderStatusOK = false;
    if (!booCurrentSagemReaderStatusOK)
        booPreviousSagemReaderStatusOK = booCurrentSagemReaderStatusOK;
}

我的第一直觉是 oblSagem 没有初始化变量。 因为它突出了错误行:

  oblSagem = SagemReaderAPI.InitSagemReader();

内部异常错误突显了这一点:

CommonMethods.LogError("MemberInfo()", "[SEGAM ERROR]" + ex);

显示错误消息:

Exception thrown: 'System.NullReferenceException' in Common.dll

Additional information: Log Master must be initiate before this method. Check static constructor.

我尝试初始化它但不起作用。 那是我犯错的地方吗?

1 个答案:

答案 0 :(得分:2)

抓住异常。看看.InnerException。它会告诉你问题是什么。它将与static SagemReaderAPI() {...}的静态构造函数(static int _someField = DoSomeStuff();或字段初始值设定项(如SagemReader.SagemReaderAPI的静态字段)相关,但没有该代码...