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);
                    }
                }
            }
        }