private protected virtual object GetSection(object newObject) { foreach (FieldInfo field in newObject.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance | BindingFlags.Instance)) { if (!(field.GetCustomAttribute <NonSerializedAttribute>() is null)) { continue; } RegistryKeySerializableAttribute registryKeyAttribute = field.GetCustomAttribute <RegistryKeySerializableAttribute>(); RegistrySubSectionSerializableAttribute registrySubSectionAttribute = field.GetCustomAttribute <RegistrySubSectionSerializableAttribute>(); if (!(registrySubSectionAttribute is null)) { object currentFieldValue = field.FieldType.GetConstructor(Type.EmptyTypes).Invoke(null); field.SetValue(newObject, currentFieldValue); GetSubSection(field.FieldType).GetSection(field.GetValue(newObject)); } string fieldName = field.Name; if (registryKeyAttribute is null && registrySubSectionAttribute is null) { field.SetValue(newObject, m_CurrentSection.GetValue(fieldName)); }
public SectionManager(Type type, RegistryKey mainRegistrySection) { m_EntityType = type; m_MainRegistrySection = mainRegistrySection; RegistrySectionSerializableAttribute registrySectionAttribute = m_EntityType.GetCustomAttribute <RegistrySectionSerializableAttribute>(); if (registrySectionAttribute is null) { throw new InvalidOperationException(); } m_EntityName = string.IsNullOrWhiteSpace(registrySectionAttribute.SectionName) ? m_EntityType.Name : registrySectionAttribute.SectionName; m_SubSections = new Lazy <List <SubSectionManager> >(); m_CurrentSection = m_MainRegistrySection.CreateSubKey(m_EntityName); m_EntityType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance | BindingFlags.Instance).ToList().ForEach(field => { RegistrySubSectionSerializableAttribute registrySubSectionAttribute = field.GetCustomAttribute <RegistrySubSectionSerializableAttribute>(); if (!(registrySubSectionAttribute is null)) { m_SubSections.Value.Add(new SubSectionManager(field.FieldType, m_CurrentSection)); } }); }