简体   繁体   English

System.TypeInitializationException

[英]System.TypeInitializationException

I am writing tests to test Infopath Forms to open in Form Control, my test method is as 我正在编写测试来测试Infopath Forms在Form Control中打开,我的测试方法是

[TestMethod]
public void Validate_OpenInfopathInFormControl()
{
    Helper.OpenForm();
    //Other Code    
}

I have written Helper class as 我写过Helper课程

public class Helper
{  
    public static void OpenForm()
    {
        //Code to Open Form
    }
}

But everytime I execute this code, this gives me: 但每次我执行此代码时,这都给了我:

Test method InfoPathTest.TestAPI.Validate_OpenInfopathInFormControl threw exception: System.TypeInitializationException: The type initializer for 'InfoPathTest.Helpers.Helper' threw an exception. 测试方法InfoPathTest.TestAPI.Validate_OpenInfopathInFormControl抛出异常:System.TypeInitializationException:'InfoPathTest.Helpers.Helper'的类型初始值设定项引发异常。 ---> System.NullReferenceException: Object reference not set to an instance of an object.. ---> System.NullReferenceException:对象引用未设置为对象的实例..

When I try to debug, it fails when Helper class needs to be initialized. 当我尝试调试时,当Helper类需要初始化时失败。 This is really eating my head, is there any solution for this? 这真的是在吃我的脑袋,对此有什么解决方案吗?

Here is the complete helper class: 这是完整的助手类:

namespace InfoPathTest.Helpers
{
    public class Helper
    {
    //This is the form i need to OPEN
        private static MainForm f =  new MainForm();
        private static bool _isOpen = false;

        public static bool isOpen
        {
            set { _isOpen = value; }
            get { return _isOpen; }
        }

        public static void OpenForm()
        {
            try
            {
                f.Show();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            _isOpen = true;

        }

        public static void CloseForm()
        {
            f.Hide();
        }
    }
}

Your test calls Helper.OpenForm() and as you have no static constructor, the only thing I can see that would cause the exception to be thrown is: 你的测试调用了Helper.OpenForm() ,因为你没有静态构造函数,我唯一能看到会导致抛出异常的是:

private static MainForm f =  new MainForm();

Therefore something in the constructor for MainForm is likely throwing an exception. 因此,MainForm的构造函数中的某些内容可能会引发异常。 Put a breakpoint on the first line of the constructor for MainForm and step through until you see where the exception is thrown. 在MainForm的构造函数的第一行放置一个断点,然后逐步执行,直到看到抛出异常的位置。

Alternatively you might find it easier to determine what the problem is, at least initially, by writing a new test you can step through that calls new MainForm() directly: 或者,您可能会发现更容易确定问题是什么,至少在最初,通过编写一个新的测试,您可以直接调用new MainForm()

[TestMethod]
public void Validate_OpenInfopathInFormControl()
{
    var form = new MainForm();
}

Put a breakpoint on the only line of the test and step into the constructor to determine why it's throwing a NullReferenceException . 在测试的唯一一行放置断点并进入构造函数以确定它为什么抛出NullReferenceException

The type initialiser, in this case, is where your static fields are initialised; 在这种情况下,类型初始化器是初始化静态字段的位置; That is, these two lines: 也就是说,这两行:

private static MainForm f =  new MainForm();
private static bool _isOpen = false;

The initialisation of a bool isn't going to cause any kind of exception, so it's highly likely that the source of the error is in the MainForm constructor. bool的初始化不会导致任何类型的异常,因此错误的来源很可能是在MainForm构造函数中。

Does the TypeInitializationException object contain any inner exceptions? TypeInitializationException对象是否包含任何内部异常? If so, they should give you more info about the real cause of the error. 如果是这样,他们应该给你更多关于错误的真正原因的信息。

You have an error in your static constructor (they are called Type Initializers). 您的静态构造函数中有一个错误(它们被称为Type Initializers)。 The inner exception is a NullReference exception. 内部异常是NullReference异常。 If you post your code we might be able to help you. 如果您发布代码,我们可能会帮助您。

The rules determine when type initializers get run are complex, but it is guaranteed that they are run before you access the type in any way. 规则确定何时运行类型初始化程序很复杂,但保证在以任何方式访问类型之前运行它们。 It might not be directly obvious to you that you have a type initializer on your Helper class because you might use implicit initialization: 您可能没有直接明白Helper类上有类型初始值设定项,因为您可能使用隐式初始化:

public class Helper
{   
    static int i = 10; // This assignment will end up in a type initializer
    static Helper()
    {
        // Explicit type initializer
        // The compiler will make sure both, implicit and explicit initializations are run
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM