C++结构struct st
{
  int ivalue1;//长度=4
  char cvalue2[4];//长度=4
}struct stEx
{
  int ivalue1;//4
  st  stvalue2;//8
}
在C#NET里怎样计算里面存储的长度,并根据这个结构可以反序列化.目前我用的是序列化长度是有问题的.

解决方案 »

  1.   

    序列化和反序列化 .net中不是有现成解决方案吗~~~
    给类加一个"可序列化"的特性就行了.....具体...搜索吧...
      

  2.   

    http://www.cnblogs.com/qqflying/archive/2008/01/13/1037262.html
      

  3.   

    sizeof() only for value type instead of reference type (managed type) in C#.Ding!
      

  4.   

    谢谢楼上回答
    我的主要问题是长度问题.不是序列化问题.我看过你的链接,SOAP里序列化出来的是XML数据.
    3楼的SIZEOF我不太理解.请明示
      

  5.   

    C#中sizeof只能针对值类型
    struct MyStruct
    {
        public int i;
        public double b;
    }
    class main
    {
        static unsafe void Main()
        {
            Console.WriteLine(sizeof(MyStruct)); //output 16:struct-4 int-4 double-8
        }
    }
      

  6.   

    不明白你说的长度什么意思,你自己 不是把长度写出来了吗?8位一个byte,C++和C#都是一样的。
      

  7.   

    比如,C++里面int 是32位的,所以长度是4,而C#的INT也是32位的,所以长度还是4,其他的有些不一样的,你去看看SDK就知道了,。
      

  8.   

    sizeof 运算符用于获取 值类型 的字节大小。例如,可以如下所示检索 int 类型的大小:
    int intSize = sizeof(int);
     
    sizeof 运算符仅适用于值类型,而不适用于引用类型。
    注意 
    从 C# 2.0 版开始,将 sizeof 应用于预定义类型不再要求使用 不安全 模式。
     不能重载 sizeof 运算符。由 sizeof 运算符返回的值是 int 类型。下表显示表示某些预定义类型大小的常数值。表达式
     结果
     
    sizeof(sbyte)
     1
     
    sizeof(byte)
     1
     
    sizeof(short)
     2
     
    sizeof(ushort)
     2
     
    sizeof(int)
     4
     
    sizeof(uint)
     4
     
    sizeof(long)
     8
     
    sizeof(ulong)
     8
     
    sizeof(char)
     2 (Unicode)
     
    sizeof(float)
     4
     
    sizeof(double)
     8
     
    sizeof(bool)
     1
     对于所有其他类型(包括 struct),sizeof 运算符只能在不安全代码块中使用。虽然可以使用 SizeOf 方法,但该方法返回的值和 sizeof 返回的值并不总是相同的。Marshal.SizeOf 在已封送处理类型后返回大小,而 sizeof 返回公共语言运行库分配的大小(包括任何空白)。示例
    // cs_operator_sizeof.cs
    // compile with: /unsafe
    using System;
    class MainClass
    {
        unsafe static void Main()
        {
            Console.WriteLine("The size of short is {0}.", sizeof(short));
            Console.WriteLine("The size of int is {0}.", sizeof(int));
            Console.WriteLine("The size of long is {0}.", sizeof(long));
        }
    } 输出
      
    The size of short is 2.
    The size of int is 4.
    The size of long is 8.
      

  9.   

    Marshal 类  
    提供了一个方法集,这些方法用于分配非托管内存、复制非托管内存块、将托管类型转换为非托管类型,此外还提供了在与非托管代码交互时使用的其他杂项方法。 命名空间:System.Runtime.InteropServices备注
    Marshal 类中定义的 static 方法对于处理非托管代码至关重要。此类中定义的大多数方法通常由需要在托管和非托管编程模型之间提供桥梁的开发人员使用。例如,StringToHGlobalAnsi 方法将 ANSI 字符从指定的字符串(在托管堆中)复制到非托管堆中的缓冲区。该方法还分配大小正确的目标堆。公共语言运行库提供了特定的封送处理功能。有关封送处理行为的详细信息,请参见 互操作封送处理。示例
    下面的代码示例演示如何使用 Marshal 类所定义的各个方法。C# 
    using System;
    using System.Text;
    using System.Runtime.InteropServices;public struct Point
    {
        public Int32 x, y;
    }
    public sealed class App
    {
        static void Main()
        {
            // Demonstrate the use of public static fields of the Marshal class.
            Console.WriteLine("SystemDefaultCharSize={0}, SystemMaxDBCSCharSize={1}",
                Marshal.SystemDefaultCharSize, Marshal.SystemMaxDBCSCharSize);        // Demonstrate the use of the SizeOf method of the Marshal class.
            Console.WriteLine("Number of bytes needed by a Point object: {0}", 
                Marshal.SizeOf(typeof(Point)));
            Point p = new Point();
            Console.WriteLine("Number of bytes needed by a Point object: {0}",
                Marshal.SizeOf(p));
            
            // Demonstrate how to call GlobalAlloc and 
            // GlobalFree using the Marshal class.
            IntPtr hglobal = Marshal.AllocHGlobal(100);
            Marshal.FreeHGlobal(hglobal);        // Demonstrate how to use the Marshal class to get the Win32 error 
            // code when a Win32 method fails.
            Boolean f = CloseHandle(new IntPtr(-1));
            if (!f)
            {
                Console.WriteLine("CloseHandle call failed with an error code of: {0}", 
                    Marshal.GetLastWin32Error());
            }  
        }    // This is a platform invoke prototype. SetLastError is true, which allows 
        // the GetLastWin32Error method of the Marshal class to work correctly.    
        [DllImport("Kernel32", ExactSpelling = true, SetLastError = true)]
        static extern Boolean CloseHandle(IntPtr h);
        
    }// This code produces the following output.
    // 
    // SystemDefaultCharSize=2, SystemMaxDBCSCharSize=1
    // Number of bytes needed by a Point object: 8
    // Number of bytes needed by a Point object: 8
    // CloseHandle call failed with an error code of: 6
      

  10.   

    谢谢兄弟位的回答.这个贴子刷新了一下.刷出两个贴子.
    热心的兄弟请到
    http://topic.csdn.net/u/20080122/11/203dc4a9-88b3-433c-bed7-67540538e7b4.html?seed=654938313
    回答谢谢~~这里分照样会结
      

  11.   


    Console.WriteLine(Marshal.SizeOf(typeof(stEx)).ToString());