private static object GetFieldValue(byte[] block, DataAttribute d, Type t) { byte[] tmp = new byte[d.Length]; unchecked { if (typeof(string) == t) { if (d.Length <= 0) { throw new IndexOutOfRangeException("FIXME: For strings, Length must be > 0!"); } //Okay. This should be relatively easy. string s = d.Encoding.GetString(block, d.Offset, d.Length); //Tada! return(s); } else if ((typeof(int) == t) || typeof(uint) == t) { //Ignore d.Length here, its 4 bytes, Period. uint ret = 0; for (int i = 0; i < 4; i++) { ret = ret << 8; ret += block[(3 - i) + d.Offset]; } if (typeof(int) == t) { return((int)ret); } return(ret); } else if (typeof(byte) == t) { //Here we ignore length all-together.. lmao. return(block[d.Offset]); } else if (typeof(byte[]) == t) { //Ok, Piece of cake. byte[] ret = new byte[d.Length]; Array.Copy(block, d.Offset, ret, 0, d.Length); return(ret); } else if ((typeof(ushort) == t) || (typeof(short) == t)) { //Again we ignore the length all-together. Always 2 bytes, Period. ushort ret = 0; for (int i = 0; i < 2; i++) { ret = (ushort)(ret << 8); ret += block[(1 - i) + d.Offset]; } if (typeof(short) == t) { return((short)ret); } return(ret); } } //If we get here, It means we don't know this data type! throw new InvalidCastException( "FIXME: I don't know how to handle" + " this type! (" + t.ToString() + ")"); }
private static void FillSingle <T>(byte[] block, DataAttribute descriptor, FieldInfo fi, T target) { object val = GetFieldValue(block, descriptor, fi.FieldType); fi.SetValue(target, val); }