private static void ReadProperties(Type tyObject, object readValue, hid_t groupId) { PropertyInfo[] miMembers = tyObject.GetProperties(/*BindingFlags.DeclaredOnly |*/ BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo info in miMembers) { bool nextInfo = false; foreach (Attribute attr in Attribute.GetCustomAttributes(info)) { Hdf5Save kind = (attr as Hdf5SaveAttribute).SaveKind; nextInfo = (kind == Hdf5Save.DoNotSave); } if (nextInfo) { continue; } Type ty = info.PropertyType; TypeCode code = Type.GetTypeCode(ty); string name = info.Name; Trace.WriteLine($"groupname: {tyObject.Name}; property name: {name}"); if (ty.IsArray) { var elType = ty.GetElementType(); TypeCode elCode = Type.GetTypeCode(elType); Array values; if (elCode != TypeCode.Object || ty == typeof(TimeSpan[])) { values = dsetRW.ReadArray(elType, groupId, name); } else { var obj = CallByReflection(nameof(ReadCompounds), elType, new object[] { groupId, name }); var objArr = ((IEnumerable)obj).Cast <object>().ToArray(); values = Array.CreateInstance(elType, objArr.Length); Array.Copy(objArr, values, objArr.Length); } info.SetValue(readValue, values); } else if (primitiveTypes.Contains(code) || ty == typeof(TimeSpan)) { Array values = dsetRW.ReadArray(ty, groupId, name); int[] first = new int[values.Rank].Select(f => 0).ToArray(); info.SetValue(readValue, values.GetValue(first)); } else { Object value = info.GetValue(readValue, null); if (value != null) { value = ReadObject(groupId, value, name); info.SetValue(readValue, value); } } } }
public static object WriteObject(long groupId, object writeValue, string groupName = null) { if (writeValue == null) { throw new ArgumentNullException(nameof(writeValue)); } Type tyObject = writeValue.GetType(); Attribute attribute = Attribute.GetCustomAttributes(tyObject).SingleOrDefault(a => a is Hdf5GroupName); if (string.IsNullOrEmpty(groupName) && attribute != null) { groupName = ((Hdf5GroupName)attribute).Name; } bool createGroupName = !string.IsNullOrWhiteSpace(groupName); if (createGroupName) { groupId = CreateOrOpenGroup(groupId, groupName); } foreach (Attribute attr in Attribute.GetCustomAttributes(tyObject)) { if (attr is Hdf5SaveAttribute legAt) { Hdf5Save kind = legAt.SaveKind; if (kind == Hdf5Save.DoNotSave) { return(writeValue); } } } WriteProperties(tyObject, writeValue, groupId); WriteFields(tyObject, writeValue, groupId); WriteHdf5Attributes(tyObject, groupId, groupName); if (createGroupName) { CloseGroup(groupId); } return(writeValue); }
public static object WriteObject(hid_t groupId, object writeValue, string groupName = null) { if (writeValue == null) { throw new ArgumentNullException(nameof(writeValue)); } bool createGroupName = !string.IsNullOrWhiteSpace(groupName); if (createGroupName) { groupId = Hdf5.CreateGroup(groupId, groupName); } Type tyObject = writeValue.GetType(); foreach (Attribute attr in Attribute.GetCustomAttributes(tyObject)) { Hdf5SaveAttribute legAt = attr as Hdf5SaveAttribute; if (legAt != null) { Hdf5Save kind = legAt.SaveKind; if (kind == Hdf5Save.DoNotSave) { return(writeValue); } } } WriteProperties(tyObject, writeValue, groupId); WriteFields(tyObject, writeValue, groupId); WriteHdf5Attributes(tyObject, groupId, groupName); if (createGroupName) { Hdf5.CloseGroup(groupId); } return(writeValue); }
private static void ReadFields(Type tyObject, object readValue, hid_t groupId) { FieldInfo[] miMembers = tyObject.GetFields(BindingFlags.DeclaredOnly | /*BindingFlags.NonPublic |*/ BindingFlags.Public | BindingFlags.Instance); foreach (FieldInfo info in miMembers) { bool nextInfo = false; foreach (Attribute attr in Attribute.GetCustomAttributes(info)) { if (attr is Hdf5SaveAttribute) { Hdf5Save kind = (attr as Hdf5SaveAttribute).SaveKind; nextInfo = (kind == Hdf5Save.DoNotSave); } else { nextInfo = false; } } if (nextInfo) { continue; } Type ty = info.FieldType; TypeCode code = Type.GetTypeCode(ty); string name = info.Name; if (ty.IsArray) { var elType = ty.GetElementType(); TypeCode elCode = Type.GetTypeCode(elType); Array values; if (elCode != TypeCode.Object) { values = dsetRW.ReadArray(ty, groupId, name); } else { var obj = CallByReflection(nameof(ReadCompounds), elType, new object[] { groupId, name }); values = (Array)obj; } info.SetValue(readValue, values); } else if (primitiveTypes.Contains(code) || ty == typeof(TimeSpan)) { Array values = dsetRW.ReadArray(ty, groupId, name); // get first value depending on rank of the matrix int[] first = new int[values.Rank].Select(f => 0).ToArray(); info.SetValue(readValue, values.GetValue(first)); } else { Object value = info.GetValue(readValue); if (value != null) { ReadObject(groupId, value, name); } } } }
private static void ReadFields(Type tyObject, object readValue, long groupId) { FieldInfo[] miMembers = tyObject.GetFields(BindingFlags.DeclaredOnly | /*BindingFlags.NonPublic |*/ BindingFlags.Public | BindingFlags.Instance); foreach (FieldInfo info in miMembers) { bool nextInfo = false; string alternativeName = string.Empty; foreach (Attribute attr in Attribute.GetCustomAttributes(info)) { if (attr is Hdf5EntryNameAttribute nameAttribute) { alternativeName = nameAttribute.Name; } if (attr is Hdf5SaveAttribute attribute) { Hdf5Save kind = attribute.SaveKind; nextInfo = (kind == Hdf5Save.DoNotSave); } else { nextInfo = false; } } if (nextInfo) { continue; } Type ty = info.FieldType; TypeCode code = Type.GetTypeCode(ty); string name = info.Name; Hdf5Utils.LogDebug?.Invoke($"groupname: {tyObject.Name}; field name: {name}"); bool success; Array values; if (ty.IsArray) { var elType = ty.GetElementType(); TypeCode elCode = Type.GetTypeCode(elType); if (elCode != TypeCode.Object) { (success, values) = dsetRW.ReadArray(elType, groupId, name, alternativeName); } else { (success, values) = CallByReflection <(bool, Array)>(nameof(ReadCompounds), elType, new object[] { groupId, name }); } if (success) { info.SetValue(readValue, values); } } else if (primitiveTypes.Contains(code) || ty == typeof(TimeSpan)) { (success, values) = dsetRW.ReadArray(ty, groupId, name, alternativeName); // get first value depending on rank of the matrix int[] first = new int[values.Rank].Select(f => 0).ToArray(); if (success) { info.SetValue(readValue, values.GetValue(first)); } } else { object value = info.GetValue(readValue); if (value != null) { ReadObject(groupId, value, name); } } } }
private static void ReadProperties(Type tyObject, object readValue, long groupId) { PropertyInfo[] miMembers = tyObject.GetProperties(/*BindingFlags.DeclaredOnly |*/ BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo info in miMembers) { bool nextInfo = false; string alternativeName = string.Empty; foreach (Attribute attr in Attribute.GetCustomAttributes(info)) { if (attr is Hdf5SaveAttribute hdf5SaveAttribute) { Hdf5Save kind = hdf5SaveAttribute.SaveKind; nextInfo = (kind == Hdf5Save.DoNotSave); } if (attr is Hdf5EntryNameAttribute hdf5EntryNameAttribute) { alternativeName = hdf5EntryNameAttribute.Name; } } if (nextInfo) { continue; } Type ty = info.PropertyType; TypeCode code = Type.GetTypeCode(ty); string name = info.Name; bool success; Array values; if (ty.IsArray) { var elType = ty.GetElementType(); TypeCode elCode = Type.GetTypeCode(elType); if (elCode != TypeCode.Object || ty == typeof(TimeSpan[])) { (success, values) = dsetRW.ReadArray(elType, groupId, name, alternativeName); if (success) { info.SetValue(readValue, values); } } else { var obj = CallByReflection <IEnumerable>(nameof(ReadCompounds), elType, new object[] { groupId, name }); var objArr = (obj).Cast <object>().ToArray(); values = Array.CreateInstance(elType, objArr.Length); Array.Copy(objArr, values, objArr.Length); info.SetValue(readValue, values); } } else if (primitiveTypes.Contains(code) || ty == typeof(TimeSpan)) { (success, values) = dsetRW.ReadArray(ty, groupId, name, alternativeName); if (success && values.Length > 0) { int[] first = new int[values.Rank].Select(f => 0).ToArray(); info.SetValue(readValue, values.GetValue(first)); } } else { object value = info.GetValue(readValue, null); if (value != null) { value = ReadObject(groupId, value, name); info.SetValue(readValue, value); } } } }
public Hdf5Save SaveKind => saveKind; // Topic is a named parameter public Hdf5SaveAttribute(Hdf5Save saveKind) // url is a positional parameter { this.saveKind = saveKind; }
private static void ReadProperties(Type tyObject, object readValue, long groupId) { PropertyInfo[] miMembers = tyObject.GetProperties( /*BindingFlags.DeclaredOnly |*/ BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo info in miMembers) { bool nextInfo = false; string alternativeName = string.Empty; foreach (Attribute attr in Attribute.GetCustomAttributes(info)) { if (attr is Hdf5SaveAttribute hdf5SaveAttribute) { Hdf5Save kind = hdf5SaveAttribute.SaveKind; nextInfo = (kind == Hdf5Save.DoNotSave); } if (attr is Hdf5EntryNameAttribute hdf5EntryNameAttribute) { alternativeName = hdf5EntryNameAttribute.Name; } } if (nextInfo) { continue; } Type ty = info.PropertyType; TypeCode code = Type.GetTypeCode(ty); string name = info.Name; bool success; Array values; if (ty.IsArray) { var elType = ty.GetElementType(); TypeCode elCode = Type.GetTypeCode(elType); if (elCode != TypeCode.Object || ty == typeof(TimeSpan[])) { (success, values) = dsetRW.ReadArray(elType, groupId, name, alternativeName); if (success) { info.SetValue(readValue, values); } } else { var obj = CallByReflection <IEnumerable>(nameof(ReadCompounds), elType, new object[] { groupId, name, alternativeName }); var objArr = (obj).Cast <object>().ToArray(); values = Array.CreateInstance(elType, objArr.Length); Array.Copy(objArr, values, objArr.Length); info.SetValue(readValue, values); } } else if (ty.IsGenericType && ty.GetGenericTypeDefinition() == typeof(List <>)) { var elType = Hdf5Utils.GetEnumerableType(ty); TypeCode elCode = Type.GetTypeCode(elType); if (elCode != TypeCode.Object) { (success, values) = dsetRW.ReadArray(elType, groupId, name, alternativeName); if (success) { Type genericClass = typeof(List <>); // MakeGenericType is badly named Type constructedClass = genericClass.MakeGenericType(elType); IList created = (IList)Activator.CreateInstance(constructedClass); foreach (var o in values) { created.Add(o); } info.SetValue(readValue, created); } } else { var result = CallByReflection <object>(nameof(ReadCompounds), elType, new object[] { groupId, name, alternativeName }); info.SetValue(readValue, result); } } else if (primitiveTypes.Contains(code) || ty == typeof(TimeSpan)) { (success, values) = dsetRW.ReadArray(ty, groupId, name, alternativeName); if (success && values.Length > 0) { int[] first = new int[values.Rank].Select(f => 0).ToArray(); if (info.CanWrite) { info.SetValue(readValue, values.GetValue(first)); } else { Hdf5Utils.LogMessage($"property {info.Name} is read only. cannot set value", Hdf5LogLevel.Warning); } } } else { object value = info.GetValue(readValue, null); if (value != null) { value = ReadObject(groupId, value, name); info.SetValue(readValue, value); } } } }