C# 类型初始化异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12425199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:14:26  来源:igfitidea点击:

Type initialization exception

c#.netopencvconstructoremgucv

提问by Michael

I created imageHolder class:

我创建了 imageHolder 类:

public class ImageHolder : Image<Bgr, Byte>
{   
    private String imagePath;

    public ImageHolder(String path):base(path)
    {
       this.imagePath = path;                     
    }   
    public String imgPathProperty
    {
        get
        { return imagePath; }
        set
        { imagePath = value; }
    }
}

I create instance of the class and initialize it,like this:

我创建类的实例并初始化它,如下所示:

private ImageHolder originalImageHolder;
originalImageHolder = new ImageHolder(openFileDialog.FileName);

In runtime i get this exception:

在运行时我得到这个异常:

The type initializer for 'Emgu.CV.CvInvoke' threw an exception.

'Emgu.CV.CvInvoke' 的类型初始值设定项抛出异常。

enter image description here

在此处输入图片说明

Here is Solution Explorer window:

这是解决方案资源管理器窗口:

enter image description here

在此处输入图片说明

Any idea why i get this exception and how can i fix it?

知道为什么我会收到此异常以及如何修复它吗?

Thank you in advance.

先感谢您。

采纳答案by Justin

The TypeInitializationException (the exception that you are seeing) is thrown whenever a static constructor throws an exception, or whenever you attempt to access a class where the static constructor threw an exception - its InnerExceptionproperty is the property that contains the detail of the exception that was actualy thrown - this is the exception that you need to investigate.

TypeInitializationException(不同的是,你所看到的)被抛出时静态构造函数抛出一个异常,或当您试图访问一个类,其中静态构造函数抛出异常-它的InnerException属性是一个包含了异常的细节财产实际上被抛出 - 这是您需要调查的异常。

In this case from your screenshot the problem appears to be that the DLL "opencv_core240.dll" could not be found. This could be for a number of reasons

在这种情况下,从您的屏幕截图来看,问题似乎是找不到 DLL“opencv_core240.dll”。这可能有多种原因

  • The DLL couldn't be found
  • One of the dependencies of the DLL could not be found
  • The DLL was in the incorrect image format (32 bit as opposed to 64 bit)
  • 找不到 DLL
  • 找不到 DLL 的依赖项之一
  • DLL 的图像格式不正确(32 位而不是 64 位)

I'd suggest that you take a look at this questionto see if any of the suggestions on there help you.

我建议你看看这个问题,看看那里的任何建议是否对你有帮助。

回答by Michael

I solved the problem by reinstalling MSVCRT 9.0 SP1 x86

我通过重新安装MSVCRT 9.0 SP1 x86解决了这个问题

回答by SAm

VS Project Properties Page

VS 项目属性页面

Checking this field did the trick for me. Under Project→ Properties→ Build (Main/Startup project)

检查这个字段对我有用。在Project→ Properties → Build (Main/Startup project)