//类定义
    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: 

解决方案 »

  1.   

    不知道是什么原因,结构的定义是C++那边定义好的.h文件
    在C#里这样定义就会挂掉
      

  2.   

    编译是可以通过,但是DEBUG方式运行的时候就报错了。VS2005、Vs2008、VS2010都调过
      

  3.   

    上面的代码可是太多,我发个简化的代码,大家伙帮我看看为问题出在哪里。代码的目的是结构里镶套结构。[code]using System.Runtime.InteropServices;namespace ConsoleApplication3
    {      public class Classxxoo
        {          [StructLayout(LayoutKind.Explicit , Size = 4)]
              private struct Block_File
              {
                  [FieldOffset(0)]
                  public int frameNum;      
              }          [StructLayout(LayoutKind.Explicit , Size = 8)]
              private struct clipIdx_info_t
              {
                  [FieldOffset(0)]
                  public int clip_off;   
                  [FieldOffset(4),MarshalAs(UnmanagedType.ByValArray,SizeConst=1  )]
                  public Block_File blkfile;
              }
            [StructLayout(LayoutKind.Explicit , Size = 16)]
            private struct blockIdx_info_t
            {
                [FieldOffset(0) , 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();
                
            }
        }[/code]
      

  4.   

    在VS2010上DEBUG没问题。。楼主单独创建个项目,把你上面的代码粘贴上跑下喃?只有你上面的这些代码确实跑不出问题啊
      

  5.   

    按照lz的写法,结构体是Classxxoo类的成员,类的初始化会先初始化变量m_FileIndex,这时候结构体定义都还没有。建议lz将结构体定义写在类的外面,与类平级。注意不能加private修饰符。
      

  6.   

    对,不要加private ,或你改成static已没问题的。
      

  7.   

    刚才亲自测试通过,是VS2010,FM 4.0可能运行。
      

  8.   

    为啥就只有4.0才可以呢,这个和framework有什么关联
      

  9.   

    既然4.0支持dynamic,想必运行的时候没有去考虑类成员的完整性。
      

  10.   

    见鬼了,我怎么也运行不址。一运行到实例化Classxxoo的时候,IDE就报错了。我是在Win7 32位下试的。
      

  11.   


    //结构体定义
            //[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();
            }
        }