System.TypeLoadException:未能加载类型从程序集“X”“Y” [英] System.TypeLoadException: Could not load type 'x' from assembly 'y'
问题描述
我试图端口一个程序从VB6到C#读取二进制文件,并解析它。我没有得到任何编译时错误或警告但是当我尝试运行它,它甚至进入的Main()
它抛出异常
I am trying to port a program from VB6 to C# that reads in a binary file and parses it. I get no compile time errors or warnings however when I try to run it, before it even enters Main()
it throws the exception
System.TypeLoadException was unhandled
Message=Could not load type 'Conversion.DataStructures.ClientOld' from assembly
'SandboxConsole, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because
it contains an object field at offset 1 that is incorrectly aligned or overlapped
by a non-object field.
Source=SandboxConsole
TypeName=Conversion.DataStructures.ClientOld
StackTrace:
at sandbox.Program.Main(String[] args)
at System.AppDomain._nExecuteAssembly(RuntimeAssembly 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, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
下面是旧VB6代码示例
Here is a sample of the old VB6 code
Private Type SrcClientOld
Active As String * 1 '0
titleLength As Byte '1
title As String * 8 '2
lastNameLength As Byte '10
LastName As String * 25 '11
(...)
AddedBy As String * 3 '369
junk7 As String * 22 '372
End Type '394
和这里是我的C#代码我写了
And here is my C# code I wrote
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi, Pack = 1)]
struct ClientOld
{
[FieldOffset(0)]
public byte Active;
[FieldOffset(1)]
[MarshalAs(UnmanagedType.AnsiBStr)]
public string Title;
[FieldOffset(10)]
[MarshalAs(UnmanagedType.AnsiBStr)]
public string LastName;
(...)
[FieldOffset(368)]
[MarshalAs(UnmanagedType.AnsiBStr)]
public string AddedBy;
[FieldOffset(372)]
[MarshalAs(UnmanagedType.LPArray, SizeConst = 22)]
public byte[] Unknown7;
}
一些googleing我认为这是我失踪了<$ C后$ C>包= 1 ,但增加没有解决不了我的问题。
After some googleing I thought that it was that I was missing the Pack = 1
but adding that did not solve my issue.
这是什么做任何其他建议
Any other suggestions on what to do?
编辑:
首先系统字符是一个字节长,这里是第一个记录在文件中的十六进制转储
The first charater is one byte long, here is a hex dump of the first record in the file
A.Dr.......Smith....................
41 03 44 72 2E 00 00 00 00 00 05 53 6D 69 74 68 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
| | | | ^LastName
| | ^title ^lastNameLength
| ^titleLength
^Active
EDIT2:
我的代码更改为以下剥离所有其他可能的错误,它仍然给我相同的异常
Changing my code to the following to strip out all other possible errors it is still giving me the same exception
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi, Pack = 1)]
struct ClientOld
{
[FieldOffset(0)]
public byte Active;
[FieldOffset(1)]
[MarshalAs(UnmanagedType.AnsiBStr)]
public string Title;
}
我都尝试fieldoffset(1)和2既不工作。
I tried both fieldoffset(1) and 2 and neither work.
推荐答案
我觉得
活动作为字符串* 1
Active As String * 1
转换为
字符
这在一个Unicode语言不是1字节,所以下一个字段偏移不应该是1,但大概7,如果我的理解如下正确的:
which in a Unicode language is not 1 byte, so the next field offset should not be 1, but probably 7 if I understood the following correctly:.
说明:
下面的关键是理解的字符串:当我们写代码:
Here is the key to understanding strings: when we write the code:
昏暗str作为字符串str =帮助我们是
没有定义的Unicode字符数组
本身。我们定义称为BSTR一个
的数据类型,这是基本字符串短
的成员。一个BSTR是,事实上,
A指针,由
4字节长度字段preceeded一个空结尾的Unicode
字符数组。
Dim str As String str = "help" we are not defining a Unicode character array per se. We are defining a member of a data type called BSTR, which is short for Basic String. A BSTR is, in fact, a pointer to a null-terminated Unicode character array that is preceeded by a 4-byte length field.
编辑:
通过转储似乎第二个成员都应该有[FieldOffset(2)],因为如果没有它将以前的成员重叠。 (编辑虚惊一场,我已经看到了00那里有一个03)。
By your dump it seems that the second member should have [FieldOffset(2)] because if not it would overlap on the previous member. (edit false alarm, I have seen a 00 where there is a 03).
这篇关于System.TypeLoadException:未能加载类型从程序集“X”“Y”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!