2026-03-30 17:09:29 +09:00

139 lines
5.3 KiB
C#

using System;
using Microsoft.Win32;
using Newtonsoft.Json;
using System.Reflection;
namespace WeldingCommon.Utils
{
public static class RegistrySerializer
{
private static string _basePath;
private static string _subPath;
// 기본 경로 초기화
public static void Init(string basePath = "SOFTWARE\\ILSOS\\", string subPath = "PalletInfo\\")
{
_basePath = basePath;
_subPath = subPath;
}
public static void SaveToRegistry<T>(string objectName, T value)
{
SaveToRegistry(RegistryHive.LocalMachine, _basePath + _subPath + objectName, value);
}
public static void SaveToRegistry<T>(RegistryHive hive, string registryPath, T obj)
{
if (obj == null) throw new ArgumentNullException(nameof(obj));
using (RegistryKey baseKey = RegistryKey.OpenBaseKey(hive, RegistryView.Default))
using (RegistryKey key = baseKey.CreateSubKey(registryPath)) {
foreach (PropertyInfo prop in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)) {
if (!prop.CanRead || !prop.CanWrite) continue;
object value = prop.GetValue(obj);
if (value == null) continue;
string serializedValue = SerializePropertyValue(value);
if (serializedValue == null) continue; // 직렬화 실패 또는 제외됨
key.SetValue(prop.Name, serializedValue);
}
}
}
public static T LoadFromRegistry<T>(string objectName) where T : new()
{
string fullPath = _basePath + _subPath + objectName;
return LoadFromRegistry<T>(RegistryHive.LocalMachine, fullPath);
}
public static T LoadFromRegistry<T>(RegistryHive hive, string registryPath) where T : new()
{
T obj = new T();
using (RegistryKey baseKey = RegistryKey.OpenBaseKey(hive, RegistryView.Default))
using (RegistryKey key = baseKey.OpenSubKey(registryPath)) {
if (key == null) return obj;
foreach (PropertyInfo prop in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)) {
if (!prop.CanWrite) continue;
object regVal = key.GetValue(prop.Name);
if (regVal == null) continue;
try {
object converted = DeserializePropertyValue((string)regVal, prop.PropertyType);
prop.SetValue(obj, converted);
} catch (Exception ex) {
Console.WriteLine($"[Deserialize Error] {prop.Name} → {ex.Message}");
}
}
}
return obj;
}
public static void DeleteFromRegistry(string objectName)
{
DeleteFromRegistry(RegistryHive.LocalMachine, _basePath + _subPath + objectName);
}
public static void DeleteFromRegistry(RegistryHive hive, string registryPath)
{
try {
using (RegistryKey baseKey = RegistryKey.OpenBaseKey(hive, RegistryView.Default)) {
baseKey.DeleteSubKeyTree(registryPath, throwOnMissingSubKey: false);
Console.WriteLine($"[Registry Delete] Deleted: {registryPath}");
}
} catch (Exception ex) {
Console.WriteLine($"[Registry Delete Error] Path: {registryPath}, Error: {ex.Message}");
}
}
private static string SerializePropertyValue(object value)
{
if (value == null) return null;
Type type = value.GetType();
try {
// 기본 직렬화 가능한 타입
if (type == typeof(DateTime))
return ((DateTime)value).ToString("o");
if (type.IsEnum)
return value.ToString();
if (type.IsPrimitive || type == typeof(string) || type == typeof(decimal))
return Convert.ToString(value);
// 테스트 직렬화 시도
JsonConvert.SerializeObject(value);
return JsonConvert.SerializeObject(value);
} catch (Exception ex) {
Console.WriteLine($"[SerializePropertyValue Warning] Type: {type.FullName}, Error: {ex.Message}");
return null; // 저장 생략
}
}
private static object DeserializePropertyValue(string value, Type targetType)
{
try {
if (targetType == typeof(DateTime))
return DateTime.Parse(value, null, System.Globalization.DateTimeStyles.RoundtripKind);
if (targetType.IsEnum)
return Enum.Parse(targetType, value);
if (targetType.IsPrimitive || targetType == typeof(string) || targetType == typeof(decimal))
return Convert.ChangeType(value, targetType);
return JsonConvert.DeserializeObject(value, targetType);
} catch (Exception ex) {
Console.WriteLine($"[DeserializePropertyValue Warning] Type: {targetType.FullName}, Error: {ex.Message}");
return null;
}
}
}
}