c#windows microsoft pointofservice System.TypeInitializationException
我在同一个解决方案下有两个Windows窗体应用程序 A.EXE框架是针对x86的4.5.2版本 B.EXE框架是针对x86的3.5版本 在按钮上单击A.EXE打开B.EXE 在B.EXE中,有一个打印到EPSON TMT81的函数 在B.EXE表单加载中,我初始化打印机对象c#windows microsoft pointofservice System.TypeInitializationException,c#,winforms,printing,.net-4.0,.net-3.5,C#,Winforms,Printing,.net 4.0,.net 3.5,我在同一个解决方案下有两个Windows窗体应用程序 A.EXE框架是针对x86的4.5.2版本 B.EXE框架是针对x86的3.5版本 在按钮上单击A.EXE打开B.EXE 在B.EXE中,有一个打印到EPSON TMT81的函数 在B.EXE表单加载中,我初始化打印机对象 m_Printer = new ThermalPrinter(); 热敏打印机等级: string strLogicalName = "PosPrintTMT81"; try { //Create PosExp
m_Printer = new 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”引发异常
此问题的一个常见解决方案是通过将以下内容添加到app.config恢复到旧式.NET代码访问安全性(CAS)策略:
<configuration>
<runtime>
<NetFx40_LegacySecurityPolicy enabled="true"/>
</runtime>
</configuration>
有关详细信息,请参见(&S)
您可以尝试将其添加到两个应用的app.config中,以查看它是否解决了问题。但是,这仅适用于运行在.NET 4及以上版本中的应用程序
如果更改此设置对您来说不是一个长期解决方案,那么您的B应用程序实际上必须仅在.NET 3.5中运行。因此,您需要调查是什么导致它在.NET4中运行
也许这是由你从A打开应用程序B的特殊方式造成的——你可以提供代码
或者在你的B app.config中是否有如下内容
<startup>
<supportedRuntime version="v4.0"/>
你也可以提供app.config。没问题,迟做总比不做强;)