private bool VerificationHelper(Type elementType, int length, string errorNO) { bool retVal = true; try { FixedBufferAttribute myDAttribute = new FixedBufferAttribute(elementType, length); if (myDAttribute == null) { TestLibrary.TestFramework.LogError(errorNO + ".1", "Occurs error when Construct FixedBufferAttribute instance !"); retVal = false; } if (myDAttribute.ElementType != elementType || myDAttribute.Length != length) { TestLibrary.TestFramework.LogError(errorNO + ".2", "Construct an error FixedBufferAttribute instance !"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError(errorNO + ".0", "Unexpected exception occurs: " + e); retVal = false; } return retVal; }
public static string StructToString(Object o) { Type oType = o.GetType(); StringBuilder output = new StringBuilder(); foreach (FieldInfo field in oType.GetFields()) { output.Append(field.Name); output.Append(": "); var attr = field.GetCustomAttributes(typeof(FixedBufferAttribute), false); if (attr.Length > 0) { FixedBufferAttribute nameAttr = (FixedBufferAttribute)attr[0]; GCHandle hdl = GCHandle.Alloc(field.GetValue(o), GCHandleType.Pinned); string test = Marshal.PtrToStringAnsi(hdl.AddrOfPinnedObject()); output.Append(test); } else { output.Append(field.GetValue(o)); } output.Append(", "); } foreach (PropertyInfo field in oType.GetProperties()) { output.Append(field.Name); output.Append(": "); var attr = field.GetCustomAttributes(typeof(FixedBufferAttribute), false); if (attr.Length > 0) { FixedBufferAttribute nameAttr = (FixedBufferAttribute)attr[0]; GCHandle hdl = GCHandle.Alloc(field.GetValue(o), GCHandleType.Pinned); string test = Marshal.PtrToStringAnsi(hdl.AddrOfPinnedObject()); output.Append(test); } else { output.Append(field.GetValue(o)); } output.Append(", "); } return(output.ToString()); }
/// <summary> /// Converts the given fixed-buffer struct into a corresponding LLVM type. /// </summary> /// <param name="fba">The fixed-buffer attribute.</param> /// <param name="type">The type to convert.</param> /// <returns>The mapped LLVM type.</returns> private LLVMTypeRef GetFixedBufferType(FixedBufferAttribute fba, Type type) { Debug.Assert(fba != null, "Invalid fixed-buffer attribute"); Debug.Assert(type != null, "Invalid type"); if (!typeMapping.TryGetValue(type, out MappedType mappedType)) { var elementType = GetType(fba.ElementType); var llvmType = ArrayType(elementType, fba.Length); var fieldOffsets = new Dictionary <FieldInfo, int> { [type.GetFields()[0]] = 0 }; mappedType = new MappedType(type, llvmType, fba.Length, fieldOffsets); typeMapping.Add(type, mappedType); } return(mappedType.LLVMType); }
private static int GetSizeOf(Type t) { try { return(Marshal.SizeOf(t)); } catch { int totalSize = 0; foreach (FieldInfo field in t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { object[] attr = field.GetCustomAttributes(typeof(FixedBufferAttribute), false); if (attr.Length > 0) { FixedBufferAttribute fba = attr[0] as FixedBufferAttribute; totalSize += GetSizeOf(fba.ElementType) * fba.Length; } totalSize += GetSizeOf(field.FieldType); } return(totalSize); } }
private bool VerificationHelper(Type elementType,int length,string errorNO) { bool retVal = true; try { FixedBufferAttribute myDAttribute = new FixedBufferAttribute(elementType, length); if (myDAttribute.ElementType != elementType ) { TestLibrary.TestFramework.LogError(errorNO, "Getting ElementType has an error occured"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError(errorNO+".0", "Unexpected exception occurs: " + e); retVal = false; } return retVal; }
private bool VerificationHelper(Type elementType, int length, string errorNO) { bool retVal = true; try { FixedBufferAttribute myDAttribute = new FixedBufferAttribute(elementType, length); if (myDAttribute.Length != length) { TestLibrary.TestFramework.LogError(errorNO, "an error occurs when get Length. !"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError(errorNO + ".0", "Unexpected exception occurs: " + e); retVal = false; } return(retVal); }
private static int GetSizeOf(Type t) { try { // Note: This is in a try/catch for a reason. // A structure doesn't have to be marked as generic, to have generic types INSIDE of it. // Marshal.SizeOf will toss an exception when it can't find a size due to a generic type inside it. // Also... this just makes sure we can handle any other shenanigans the marshaler does. return(Marshal.SizeOf(t)); } catch { // So, chances are, we're using generic sub-types. // This is a good, and bad thing. // Good for STL implementations, bad for most everything else. // But for the sake of completeness, lets make this work. int totalSize = 0; foreach (FieldInfo field in t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { // Check if its a fixed-size-buffer. Eg; fixed byte Pad[50]; object[] attr = field.GetCustomAttributes(typeof(FixedBufferAttribute), false); if (attr.Length > 0) { FixedBufferAttribute fba = attr[0] as FixedBufferAttribute; if (fba != null) { totalSize += GetSizeOf(fba.ElementType) * fba.Length; } } // Recursive. We want to allow ourselves to dive back into this function if we need to! totalSize += GetSizeOf(field.FieldType); } return(totalSize); } }
public static object SetValue(object o, Type type, FixedBufferAttribute attr, int index, object value) { GCHandle handle = GCHandle.Alloc(o, GCHandleType.Pinned); IntPtr source = handle.AddrOfPinnedObject(); int elementSize = Marshal.SizeOf(o) / attr.Length; try { if (elementSize == 1) { if (attr.ElementType == RuntimeSerializedPropertyType.sByte) { byte byteValue = 0; sbyte sbyteValue = (sbyte)value; if (sbyteValue < 0) { byteValue = (byte)(sbyte.MaxValue - sbyteValue); } else { byteValue = (byte)sbyteValue; } Marshal.WriteByte(source, index * elementSize, byteValue); } else { Marshal.WriteByte(source, index * elementSize, Convert.ToByte(value)); } } else if (elementSize == 2) { if (attr.ElementType == RuntimeSerializedPropertyType.uShort) { short shortValue = 0; ushort ushortValue = (ushort)value; if (ushortValue > short.MaxValue) { shortValue = (short)(short.MaxValue - ushortValue); } else { shortValue = (short)ushortValue; } Marshal.WriteInt16(source, index * elementSize, shortValue); } else { Marshal.WriteInt16(source, index * elementSize, Convert.ToInt16(value)); } } else if (elementSize == 4) { if (attr.ElementType == RuntimeSerializedPropertyType.uInteger) { int intValue = 0; uint uintValue = (uint)value; if (uintValue > int.MaxValue) { intValue = (int)(int.MaxValue - uintValue); } else { intValue = (int)uintValue; } Marshal.WriteInt32(source, index * elementSize, intValue); } else if (attr.ElementType == RuntimeSerializedPropertyType.Float) { int intValue = 0; float floatValue = (float)value; if (floatValue > int.MaxValue) { intValue = (int)(int.MaxValue - floatValue); } else { intValue = (int)floatValue; } Marshal.WriteInt32(source, index * elementSize, intValue); } else { Marshal.WriteInt32(source, index * elementSize, Convert.ToInt32(value)); } } else if (elementSize == 8) { if (attr.ElementType == RuntimeSerializedPropertyType.uLong) { long longValue = 0; ulong ulongValue = (ulong)value; if (ulongValue > long.MaxValue) { longValue = (long)(long.MaxValue - ulongValue); } else { longValue = (long)ulongValue; } Marshal.WriteInt64(source, index * elementSize, longValue); } else if (attr.ElementType == RuntimeSerializedPropertyType.Double) { long longValue = 0; double doubleValue = (double)value; if (doubleValue > long.MaxValue) { longValue = (long)(long.MaxValue - doubleValue); } else { longValue = (long)doubleValue; } Marshal.WriteInt64(source, index * elementSize, longValue); } else { Marshal.WriteInt64(source, index * elementSize, Convert.ToInt64(value)); } } } finally { handle.Free(); } return(o); }
public static object GetValue(object o, FixedBufferAttribute attr, int index) { GCHandle handle = GCHandle.Alloc(o, GCHandleType.Pinned); IntPtr source = handle.AddrOfPinnedObject(); int elementSize = Marshal.SizeOf(o) / attr.Length; object value = null; try { if (elementSize == 1) { if (attr.ElementType == RuntimeSerializedPropertyType.sByte) { byte byteValue = Marshal.ReadByte(source, index * elementSize); if (byteValue > sbyte.MaxValue) { value = (sbyte)(sbyte.MaxValue - byteValue); } else { value = (sbyte)byteValue; } } else { value = Convert.ChangeType(Marshal.ReadByte(source, index * elementSize), attr.ElementType); } } else if (elementSize == 2) { if (attr.ElementType == RuntimeSerializedPropertyType.uShort) { short shortValue = Marshal.ReadInt16(source, index * elementSize); if (shortValue < 0) { value = (ushort)(short.MaxValue - shortValue); } else { value = (ushort)shortValue; } } else { value = Convert.ChangeType(Marshal.ReadInt16(source, index * elementSize), attr.ElementType); } } else if (elementSize == 4) { if (attr.ElementType == RuntimeSerializedPropertyType.uInteger) { int intValue = Marshal.ReadInt32(source, index * elementSize); if (intValue < 0) { value = (uint)(int.MaxValue - intValue); } else { value = (uint)intValue; } } else { value = Convert.ChangeType(Marshal.ReadInt32(source, index * elementSize), attr.ElementType); } } else if (elementSize == 8) { if (attr.ElementType == RuntimeSerializedPropertyType.uLong) { long longValue = Marshal.ReadInt64(source, index * elementSize); if (longValue < 0) { value = (ulong)(long.MaxValue - longValue); } else { value = (ulong)longValue; } } else { value = Convert.ChangeType(Marshal.ReadInt64(source, index * elementSize), attr.ElementType); } } } finally { handle.Free(); } return(value); }
private static void CreateLayoutRecurse(Type type, int baseOffset, List <Layout> layouts, ref int begin, ref int end, ref int typeHash) { FieldInfo[] fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); FieldData[] array = new FieldData[fields.Length]; int index = 0; while (true) { if (index == fields.Length) { Array.Sort <FieldData>(array, (a, b) => a.Offset - b.Offset); foreach (FieldData data in array) { FieldInfo field = data.Field; FixedBufferAttribute fixedBufferAttribute = GetFixedBufferAttribute(field); int num3 = baseOffset + data.Offset; if (fixedBufferAttribute != null) { int num4 = UnsafeUtility.SizeOf(fixedBufferAttribute.ElementType); int num5 = 0; while (true) { if (num5 >= fixedBufferAttribute.Length) { break; } CreateLayoutRecurse(fixedBufferAttribute.ElementType, num3 + (num5 * num4), layouts, ref begin, ref end, ref typeHash); num5++; } } else { int isEnum; if ((field.FieldType.IsPrimitive || field.FieldType.IsPointer) || field.FieldType.IsClass) { isEnum = 1; } else { isEnum = (int)field.FieldType.IsEnum; } if (isEnum == 0) { CreateLayoutRecurse(field.FieldType, num3, layouts, ref begin, ref end, ref typeHash); } else { int[] values = new int[] { num3, (int)Type.GetTypeCode(field.FieldType) }; CombineHash(ref typeHash, values); int num6 = -1; if (field.FieldType.IsPointer || field.FieldType.IsClass) { num6 = UnsafeUtility.SizeOf <PointerSize>(); } else if (field.FieldType.IsEnum) { num6 = UnsafeUtility.SizeOf(field.FieldType.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)[0].FieldType); } else { num6 = UnsafeUtility.SizeOf(field.FieldType); } if (end == num3) { end += num6; } else { Layout item = new Layout { offset = begin, count = end - begin, Aligned4 = false }; layouts.Add(item); begin = num3; end = num3 + num6; } } } } return; } array[index].Offset = UnsafeUtility.GetFieldOffset(fields[index]); array[index].Field = fields[index]; index++; } }