【疑难杂症】结构体定义会导致程序崩溃
- C# code
//类定义 public class Classxxoo { private const int MAX_CLIPENUM_IN_BLOCK = 254; private const int BLOCK_INDEX_NUM = (8 * 1024); //[StructLayout(LayoutKind.Sequential)] private struct Block_File { public uint frameNum; public uint clipStime; public uint clipEtime; public uint clip_size; public uint index_num; } //[StructLayout(LayoutKind.Sequential)] private struct clipIdx_info_t { public byte channel; public byte status; public ushort fileInfoIdx; public Block_File blkfile; //注意,这一行注释掉就正常工作了 public uint index_off; public uint clip_off; } //[StructLayout(LayoutKind.Sequential)] private struct KeyIndex { public uint cKeyOffset; public uint TimeStampSec; } //[StructLayout(LayoutKind.Sequential)] private struct blockIdx_info_t { public uint block_inf; public uint block_size; public uint block_no; public byte channel; public byte block_status; public byte block_type; public byte clip_num; public uint recover_num; public uint write_Size; public uint blockStime; public uint blockEtime; public uint index_num; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 28)] public byte[] rev; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] public clipIdx_info_t[] clip_info; } private blockIdx_info_t m_FileIndex; public Classxxoo( ) { m_FileIndex = new blockIdx_info_t(); } }//------------------运行代码------------------------ class Program { static void Main(string[] args) { Classxxoo xxoo = new Classxxoo( ); Console.WriteLine(xxoo.ToString()); Console.Read(); } }
异常提示
未处理 System.TypeLoadException
Message=未能从程序集“ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”中加载类型“ConsoleApplication1.Classxxoo”。
Source=ConsoleApplication1
TypeName=ConsoleApplication1.Classxxoo
StackTrace:
在 ConsoleApplication1.Program.Main(String[] args)
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:
[解决办法]
我将你的代码直接复制编译可以通过啊,只不过有很多“赋值未使用”“一直保持默认值”之类的警告而已
[解决办法]
在VS2010上DEBUG没问题。。楼主单独创建个项目,把你上面的代码粘贴上跑下喃?
只有你上面的这些代码确实跑不出问题啊
[解决办法]
按照lz的写法,结构体是Classxxoo类的成员,类的初始化会先初始化变量m_FileIndex,这时候结构体定义都还没有。建议lz将结构体定义写在类的外面,与类平级。注意不能加private修饰符。
[解决办法]
对,不要加private ,或你改成static已没问题的。
[解决办法]
刚才亲自测试通过,是VS2010,FM 4.0可能运行。
[解决办法]
为啥就只有4.0才可以呢,这个和framework有什么关联
[解决办法]
既然4.0支持dynamic,想必运行的时候没有去考虑类成员的完整性。
[解决办法]
- C# code
//结构体定义 //[StructLayout(LayoutKind.Sequential)] struct Block_File { public uint frameNum; public uint clipStime; public uint clipEtime; public uint clip_size; public uint index_num; } //[StructLayout(LayoutKind.Sequential)] struct clipIdx_info_t { public byte channel; public byte status; public ushort fileInfoIdx; public Block_File blkfile; //注意,这一行注释掉就正常工作了 public uint index_off; public uint clip_off; } //[StructLayout(LayoutKind.Sequential)] struct KeyIndex { public uint cKeyOffset; public uint TimeStampSec; } //[StructLayout(LayoutKind.Sequential)] struct blockIdx_info_t { public uint block_inf; public uint block_size; public uint block_no; public byte channel; public byte block_status; public byte block_type; public byte clip_num; public uint recover_num; public uint write_Size; public uint blockStime; public uint blockEtime; public uint index_num; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 28)] public byte[] rev; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] public clipIdx_info_t[] clip_info; }//类定义 public class Classxxoo { private const int MAX_CLIPENUM_IN_BLOCK = 254; private const int BLOCK_INDEX_NUM = (8 * 1024); private blockIdx_info_t m_FileIndex; public Classxxoo( ) { m_FileIndex = new blockIdx_info_t(); } }//------------------运行代码------------------------ class Program { static void Main(string[] args) { Classxxoo xxoo = new Classxxoo( ); Console.WriteLine(xxoo.ToString()); Console.Read(); } }