c# - Universal value for dictionary -


i have dictionary<key,datavalue>. problem datavalue can typeof int, float, vector2, vector3, byte, short, uint or bool. avoid boxing can't make datavalue object.

can create data type (class or struct) store data type in same byte array? enum inside data type hold type (int,float,bool...), know how interpret data in byte array?

edit: code can unsafe.

[structlayout(layoutkind.explicit)]     struct testunion  {     [fieldoffset(0)]      public int i;     [fieldoffset(0)]      public double d;     [fieldoffset(0)]      public char c;     [fieldoffset(0)]      public byte b1; } 

would work?

you use structlayout(layoutkind.explicit) , fieldoffset attributes create equivalent functionality. i'll assume meant 64-bit data structure, contain 8 bytes or 2 ints. if did mean 64-byte data structure, need define struct 64 bytes , 16 ints. (probably better use byte[] , int[].) can find information here:

http://winfx.msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_csref/html/163ab9b5-46f6-4d78-9025-f7bbba89b2e1.asp

for instance:

using system.runtime.interopservices; [structlayout(layoutkind.explicit)] struct bytearray {   [fieldoffset(0)]   public byte byte1;   [fieldoffset(1)]   public byte byte2;   [fieldoffset(2)]   public byte byte3;   [fieldoffset(3)]   public byte byte4;   [fieldoffset(4)]   public byte byte5;   [fieldoffset(5)]   public byte byte6;   [fieldoffset(6)]   public byte byte7;   [fieldoffset(7)]   public byte byte8;   [fieldoffset(0)]   public int int1;   [fieldoffset(4)]   public int int2; } 

one thing careful of endian-ness of machine if plan run on non-x86 platforms may have differing endianness. see http://en.wikipedia.org/wiki/endianness explanation.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -