Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 关于TypeLoadException的帮助_.net_Vb.net_Exception_Typeloadexception - Fatal编程技术网

.net 关于TypeLoadException的帮助

.net 关于TypeLoadException的帮助,.net,vb.net,exception,typeloadexception,.net,Vb.net,Exception,Typeloadexception,我正在编写一个.NET 3.5应用程序(WinForms),它使用外部DLL中的类,每次应用程序尝试启动时,我都会收到一个System.TypeLoadException。 以下是显示的异常情况: System.TypeLoadException was unhandled Message=Could not load type 'PolyMorph.Common.Settings' from assembly 'PolyMorph, Version=1.0.0.0, Culture=neu

我正在编写一个.NET 3.5应用程序(WinForms),它使用外部DLL中的类,每次应用程序尝试启动时,我都会收到一个
System.TypeLoadException

以下是显示的异常情况:

System.TypeLoadException was unhandled Message=Could not load type 'PolyMorph.Common.Settings' from assembly 'PolyMorph, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Source=PolyMorph TypeName=PolyMorph.Common.Settings StackTrace: at PolyMorphApp.App.Initialize() at PolyMorphApp.App.Main() at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: System.TypeLoadException未处理 Message=无法从程序集'PolyMorph,版本=1.0.0.0,区域性=中立,PublicKeyToken=null'加载类型'PolyMorph.Common.Settings'。 源=多晶型 TypeName=PolyMorph.Common.Settings 堆栈跟踪: 在PolyMorphApp.App.Initialize()处 在PolyMorphApp.App.Main()上 位于System.AppDomain.\u nexecutestAssembly(程序集,字符串[]args) 位于System.AppDomain.ExecuteAssembly(字符串汇编文件、证据汇编安全性、字符串[]args) 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()上 位于System.Threading.ThreadHelper.ThreadStart\u上下文(对象状态) 在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态) 位于System.Threading.ThreadHelper.ThreadStart()处 内部异常: 以下是我正在运行的代码:

Friend NotInheritable Class App

    <STAThread()> Shared Sub Main()
        'set the exception handlers'
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
        AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledExceptionHandler
        AddHandler Application.ThreadException, AddressOf ThreadExceptionHandler
        'initialize the application'
        App.Initialize()

        'and then run the application'
        Dim mainForm As New PolymorphHost
        Application.Run(mainForm)
    End Sub

    Shared Function Initialize() As FunctionResult
        If App.InitializeDataDirectory() = False Then
            Return New FunctionResult(False, "the application's data directory")
        End If


        _settings = New PolyMorph.Common.Settings(AppDataDirectory & "\Settings.dat")
        ......code continues to load settings from the _settings variable
    End Function
End Class
好友不可继承类应用程序
共享子主目录()
'设置异常处理程序'
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
AddHandler AppDomain.CurrentDomain.UnhandledException,UnhandledExceptionHandler的地址
AddHandler Application.ThreadException,ThreadExceptionHandler的地址
“初始化应用程序”
App.Initialize()
'然后运行应用程序'
Dim mainForm作为新的变形宿主
Application.Run(mainForm)
端接头
共享函数初始化()为FunctionResult
如果App.InitializeDataDirectory()=False,则
返回新的FunctionResult(False,“应用程序的数据目录”)
如果结束
_设置=新的PolyMorph.Common.settings(AppDataDirectory&“\settings.dat”)
……代码继续从_settings变量加载设置
端函数
末级


令我惊讶的是,VS2010调试器在
App.Initialize()
行停止,甚至没有进入
Initialize
函数

但是,如果在
Initialize
函数中注释掉对外部DLL的所有引用,则应用程序将正确初始化


在仔细阅读之后,我意识到许多报告此错误的人在他们的项目上使用了不同的构建(如从x86应用程序引用的x64 DLL)。因此,我更改了构建配置,使DLL和应用程序都是x86,但我仍然有
TypeLoadException


有什么我遗漏的吗?

最可能的原因是它在尝试加载其中一个设置时引发异常。您可以尝试在
Initialize
方法中的第一条语句处设置断点。这可能会让您有机会单步了解错误所在


或者,您可以在加载每个值后使用
Debug
语句(请参见
System.Diagnostics
)来确定失败的位置。

您应该查看InnerException和LoadException属性,以获得有关从属程序集未正确加载原因的更详细信息

它在初始化之前抛出异常的原因是由于方法是JIT编译的。当第一次执行某个方法时,CLR将验证并解析该方法中的所有MSIL指令,然后再将其编译为等效的运行时。由于Initialize方法使用PolyMorph.Common.Settings类型,因此CLR在编译时会尝试解析该类型。由于加载失败,因此从不执行初始化

要在您自己的代码中捕获异常,只需将整个初始化代码移动到另一个方法,然后从Initialize调用try…catch块中的该方法

Try
    InitializeInternal()
Catch ex As TypeLoadException
    System.Diagnostics.Debugger.WriteLine(ex.ToString())
End Try

一个Nutzy似乎也有一个问题,他通过将所有的类、窗体和控件复制到一个新项目中来解决了这个问题。我也这样做了,问题就解决了


谢谢吉姆·米谢尔和保罗·亚历山大的帮助。我投票支持你帮助我解决问题的努力。

我也有同样的问题,我确信这是VisualStudio或JIT编译器中的一个bug。我知道这与DLL或x64 vs x86无关,因为我的测试项目中没有自定义引用。我找到了结构的一个成员变量。这个特定的成员变量没有什么有趣的地方(它是一个简单的类:)

其成员如下。如果#if是真的,它就会起作用。如果我将其更改为#false,则它编译时不会出错,但在运行时,当我访问整个类中的任何内容时,它会给出TypeLoadException

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct uartparam_t
    { //size 112
        public UInt16 portnum; //0
        public Byte conntype;
        public Byte baud;
        public Byte databits;
        public Byte stopbits;
        public Byte parity;
        public Byte flowctrl;
#if true
        public int remoteip;
#else
        public Foo remoteip; //8 -- for some reason this is making the program crash! ?!?!?
#endif

     ....
其中一部分是

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct config {
 ...
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAXUARTPORT)]
        public uartparam_t[] uartparam; //(size 1792)
 ...
    }

这没有任何意义。我尝试了对应用程序进行全面重建,并在新的解决方案中创建了一个新项目,但没有任何效果。使其工作的唯一方法是将结构更改为int,并在结构之外处理从int到Foo的类型转换。

我在向产品添加代码时经常遇到此错误。我们遇到的问题是该产品的最新版本安装在GAC中,我正在尝试在我的开发人员机器上测试一个新功能。我运行的应用程序依赖于我在本地构建的新功能,但该应用程序从GAC加载程序集,其中不包含新功能。卸载产品时,异常消失。

我的问题是父程序集和DLL具有相同的程序集名称

e、 g.Application.exe和Application.dll

我在项目属性(例如Application.exe和Library.dll)中将每个名称更改为一个单独的名称,解决了这个问题。我猜选择的答案(“转移到新的解决方案”)他们将项目命名为
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct config {
 ...
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAXUARTPORT)]
        public uartparam_t[] uartparam; //(size 1792)
 ...
    }