Exemple #1
0
        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);
                    }
                }
            }
        }
Exemple #2
0
        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
                    {
                        values = CallByReflection <Array>(nameof(ReadCompounds), elType,
                                                          new object[] { groupId, name, alternativeName });
                        success = true;
                    }

                    if (success)
                    {
                        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);
                    // 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);
                    }
                }
            }
        }
Exemple #3
0
        private static void WriteField(object infoVal, Dictionary <string, List <string> > attributes, long groupId, string name)
        {
            Type     ty   = infoVal.GetType();
            TypeCode code = Type.GetTypeCode(ty);

            foreach (var attribute in Attributes(ty))
            {
                if (!attributes.ContainsKey(attribute.Key))
                {
                    attributes.Add(attribute.Key, attribute.Value);
                }
            }

            if (ty.IsArray)
            {
                var      elType = ty.GetElementType();
                TypeCode elCode = Type.GetTypeCode(elType);
                if (elCode != TypeCode.Object || ty == typeof(TimeSpan[]))
                {
                    dsetRW.WriteArray(groupId, name, (Array)infoVal, attributes);
                }
                else
                {
                    CallByReflection <(int, long)>(nameof(WriteCompounds), elType, new[] { groupId, name, infoVal, attributes });
                }
            }
            else if (ty.IsGenericType && ty.GetGenericTypeDefinition() == typeof(List <>))
            {
                var elType = Hdf5Utils.GetEnumerableType(ty);

                TypeCode elCode = Type.GetTypeCode(elType);


                if (elCode != TypeCode.Object || ty == typeof(List <TimeSpan>))
                {
                    IList infoAsList = (IList)infoVal;
                    Array arr        = Array.CreateInstance(elType, infoAsList.Count);
                    for (var i = 0; i < infoAsList.Count; i++)
                    {
                        arr.SetValue(infoAsList[i], i);
                    }
                    dsetRW.WriteArray(groupId, name, arr, attributes);
                }
                else
                {
                    CallByReflection <(int, long)>(nameof(WriteCompounds), elType, new[] { groupId, name, infoVal, attributes });
                }
            }
            else if (primitiveTypes.Contains(code) || ty == typeof(TimeSpan))
            {
                (int success, long CreatedgroupId) = //WriteOneValue(groupId, name, infoVal);

                                                     CallByReflection <(int, long)>(nameof(WriteOneValue), ty, new[] { groupId, name, infoVal, attributes });
                //todo: fix it
                //add its attributes if there are:
                //foreach (Attribute attr in Attribute.GetCustomAttributes(filedInfo))
                //{
                //    if (attr is Hdf5Attribute)
                //    {
                //        var h5at = attr as Hdf5Attribute;
                //        WriteStringAttribute(groupId, name, h5at.Name, name);
                //    }

                //    if (attr is Hdf5Attributes)
                //    {
                //        var h5ats = attr as Hdf5Attributes;
                //        WriteAttributes<string>(groupId, name, h5ats.Names, attr.);
                //    }
                //}
            }
            else
            {
                WriteObject(groupId, infoVal, name);
            }
        }