【问题标题】:c# windows microsoft pointofservice System.TypeInitializationExceptionc# windows microsoft pointofservice System.TypeInitializationException
【发布时间】:2017-09-14 19:58:04
【问题描述】:

我在同一个解决方案下有两个 Windows 窗体应用程序。

A.EXE 框架是针对 x86 的 4.5.2。

B.EXE 框架是针对 x86 的 3.5。

从 A.EXE 中单击按钮我打开 B.EXE。

在 B.EXE 中有一个打印到 EPSON TMT81 的功能。

在 B.EXE 表单加载中,我初始化了 Printer 对象...

m_Printer = new ThermalPrinter();

ThermalPrinter 类:

string strLogicalName = "PosPrintTMT81";

try
{
    //Create PosExplorer

    PosExplorer posExplorer = new PosExplorer();

    DeviceInfo deviceInfo = null;

    try
    {
        deviceInfo = posExplorer.GetDevice(DeviceType.PosPrinter, strLogicalName);
        m_Printer = (PosPrinter)posExplorer.CreateInstance(deviceInfo);
    }
    catch (Exception ex)
    {
        string strErrorMsg = ex.Message.ToString() 
            + "\n\nPlease rectify the error and try again.";
        LogException(ex);
        return strErrorMsg;
    }

PosExplorer posExplorer = new PosExplorer(); 行返回错误...

System.TypeInitializationException 的类型初始化器 'Microsoft.PointOfService.Management.Explorer' 抛出异常。

【问题讨论】:

    标签: c# winforms printing .net-4.0 .net-3.5


    【解决方案1】:

    此问题的常见解决方案是通过将以下内容添加到您的 app.config 来恢复到旧的 .NET 代码访问安全 (CAS) 策略:

    <configuration>  
        <runtime>  
            <NetFx40_LegacySecurityPolicy enabled="true"/>  
        </runtime>  
    </configuration> 
    

    请参阅herehere 了解更多信息。

    您可以尝试将其添加到两个应用的 app.config 中,看看它是否可以解决问题。但是,这仅适用于在 .NET 4 及更高版本中运行的应用程序。

    如果更改此设置不是您的长期解决方案,那么您的 B 应用程序确实必须仅在 .NET 3.5 中运行。因此,您需要调查导致它在 .NET 4 中运行的原因。

    这可能是由于您从 A 打开应用 B 的特定方式造成的 - 您可以提供该代码。

    或者您的 B app.config 中是否有类似以下内容?...

    <startup>
        <supportedRuntime version="v4.0"/> 
    

    您也可以提供 app.config。

    【讨论】:

    • 没问题,迟到总比没有好;)
    猜你喜欢
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-17
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    相关资源
    最近更新 更多