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:
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
Post a Comment