我原来是用VB写程序,会用到如下形式的数据结构体存储数据public type person
   number as long
   name as string*10
   address as string*20
end type其C#代码对应为
public struct Person

  public int number;
  public string Address;  //C#里面貌似没有定长字符串?
  public string Name; }现在,我改用C#,发现C#里面没有读取结构体并自动赋值的类库,因为数据量不大,所以不想用数据库,请问C#里面有没有针对记录体进行读写操作的类呢?? 还有C#里面有定长字符串的概念吗??

解决方案 »

  1.   


            public struct Person
            {
                public int number;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
                public string Address;  //C#里面貌似没有定长字符串? 
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
                public string Name;        } 这样看看
      

  2.   

    你的意思是? byte[]转换为结构体
    如果是参考
    Marshal.PtrToStructure 方法..
      

  3.   

    顶[align=center]****************************************************************
                  今天回帖带祝福,七夕情人节快乐~^_^
    ****************************************************************[/align=center]
      

  4.   

    也可以用System.IO.BinaryReader 
    知道格式都可以读出来
      

  5.   

    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO;//使结构可序列化
    [Serializable]
    public struct Person 

      public int number; 
      public string Address;
      public string Name; 
    } //存:
    IFormatter formatter = new BinaryFormatter();
    Person person = new Person();
    person.number = 0;
    person.Address = "123";
    pserson.Name = "456";using (Stream stream = new FileStream("C:\\a.his", FileMode.Create))
    {
        formatter.Serialize(stream, pserson);
        stream.Close();
    }
    //取
    IFormatter formatter = new BinaryFormatter();
    using (Stream stream = new FileStream("C:\\a.his", FileMode.Open))
    {
        Person person = (Person)formatter.Deserialize(stream);
        stream.Close();
    }
      

  6.   

    感谢各位鼎立相助,特殊是LixingTie的代码很好啊,还有些问题,先把帖子结了再说!