LogViewer B2 CEID=308,309 Name to ModelID

ServerLog ViewList.xml 내문서/DDUtility 폴더로 복제 및 사용
This commit is contained in:
jungwoois
2025-04-24 14:04:20 +09:00
parent de709d7030
commit 395102ffea
8 changed files with 238 additions and 245 deletions

View File

@@ -1,4 +1,6 @@
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
@@ -17,50 +19,74 @@ namespace JWH
public static bool IsDesignMode = LicenseManager.UsageMode == LicenseUsageMode.Designtime;
/// <summary>
/// 속성 정보를 캐싱하기 위한 사전입니다.
/// </summary>
private static readonly ConcurrentDictionary<Type, Dictionary<string, PropertyInfo>> PropertyCache =
new ConcurrentDictionary<Type, Dictionary<string, PropertyInfo>>();
/// <summary>
/// 주어진 타입의 속성 정보를 캐싱하여 반환합니다.
/// </summary>
/// <param name="type">대상 타입</param>
/// <returns>속성 정보 사전</returns>
private static Dictionary<string, PropertyInfo> GetCachedProperties(Type type)
{
return PropertyCache.GetOrAdd(type, t =>
t.GetProperties().ToDictionary(p => p.Name, StringComparer.OrdinalIgnoreCase));
}
#region [ PropertiesCopy ] --------------------------------------------
/// <summary>
/// 현재객체의 속성값을 대상객체의 속성에 복사합니다
/// 현재 객체의 속성 값을 대상 객체의 속성에 복사합니다.
/// </summary>
/// <param name="sender"></param>
/// <param name="dest">대상객체</param>
/// <param name="sender">원본 객체</param>
/// <param name="dest">대상 객체</param>
/// <param name="overwrite">기존 값을 덮어쓸지 여부</param>
public static void PropertiesCopy(this object sender, object dest, bool overwrite = true)
{
try
if (sender == null) throw new ArgumentNullException(nameof(sender));
if (dest == null) throw new ArgumentNullException(nameof(dest));
var sourceProperties = GetCachedProperties(sender.GetType());
var destProperties = GetCachedProperties(dest.GetType());
foreach (var sourceProp in sourceProperties)
{
foreach (PropertyInfo property in sender.GetType().GetProperties())
if (!destProperties.TryGetValue(sourceProp.Key, out var destProp)) continue;
if (!destProp.CanWrite) continue;
if (typeof(IList).IsAssignableFrom(sourceProp.Value.PropertyType))
{
try
{
PropertyInfo destProp = dest.GetType().GetProperty(property.Name, property.PropertyType);
if (destProp == null || property.GetType() != destProp.GetType()) continue;
//if (!property.CanWrite) continue;
if (!destProp.CanWrite) continue;
var sourceList = sourceProp.Value.GetValue(sender) as IList;
if (sourceList == null) continue;
var destValue = destProp.GetValue(dest);
if (overwrite == false && destValue != null) continue;
var destList = destProp.GetValue(dest) as IList ?? (IList)Activator.CreateInstance(sourceProp.Value.PropertyType);
if (overwrite) destList.Clear();
destProp.SetValue(dest, property.GetValue(sender));
}
catch (Exception ex)
foreach (var item in sourceList)
{
XLogger.Instance.Fatal(ex);
destList.Add(item);
}
destProp.SetValue(dest, destList);
}
else
{
var sourceValue = sourceProp.Value.GetValue(sender);
if (!overwrite && destProp.GetValue(dest) != null) continue;
destProp.SetValue(dest, sourceValue);
}
}
catch (Exception ex)
{
XLogger.Instance.Fatal(ex);
throw ex;
}
}
/// <summary>
/// 현재객체의 속성값을 대상객체의 속성에 복사합니다
/// DataTable의 데이터를 대상 객체의 속성에 복사합니다.
/// </summary>
/// <param name="sender"></param>
/// <param name="dest">대상객체</param>
/// <param name="sender">원본 DataTable</param>
/// <param name="dest">대상 객체</param>
public static void PropertiesCopy(this DataTable sender, object dest)
{
try
@@ -83,7 +109,6 @@ namespace JWH
catch
{
// Object of type 'System.DBNull' cannot be converted to type 'System.String'.
// XLogger.Instance.Warn(ex);
}
}
catch (Exception ex)
@@ -100,10 +125,10 @@ namespace JWH
}
/// <summary>
/// Dictionary<string, object>의 값을 대상객체의 속성에 복사합니다.
/// Dictionary의 데이터를 대상 객체의 속성에 복사합니다.
/// </summary>
/// <param name="sender"></param>
/// <param name="dest"></param>
/// <param name="sender">원본 Dictionary</param>
/// <param name="dest">대상 객체</param>
public static void PropertiesCopy(this Dictionary<string, object> sender, object dest)
{
try
@@ -134,74 +159,150 @@ namespace JWH
#endregion
#region [ Property Set/Get ] ------------------------------------------
/// <summary>
/// value 값을 현재객체의 속성에 복사합니다.
/// 주어진 값을 현재 객체의 속성에 설정합니다.
/// </summary>
/// <param name="sender"></param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="sender">대상 객체</param>
/// <param name="name">속성 이름</param>
/// <param name="value">설정할 값</param>
public static void PropertySet(this object sender, string name, object value)
{
try
{
PropertyInfo property = sender.GetType().GetProperty(name);
if (property == null)
foreach (PropertyInfo item in sender.GetType().GetProperties())
if (string.Compare(item.Name, name, true) == 0)
{
property = item;
break;
}
if (property == null) return;
if (property.PropertyType != value.GetType())
{
try
{ value = Convert.ChangeType(value, property.PropertyType); }
catch
{ return; }
}
PropertyInfo property;
GetCachedProperties(sender.GetType()).TryGetValue(name, out property);
if (property == null || !property.CanWrite) return;
property.SetValue(sender, value);
}
catch (Exception ex)
if (value != null && property.PropertyType != value.GetType())
{
XLogger.Instance.Fatal(ex);
throw ex;
value = Convert.ChangeType(value, property.PropertyType);
}
property.SetValue(sender, value);
}
/// <summary>
/// 객체의 속성값을 반환합니다.
/// 객체의 특정 속성 값을 반환합니다.
/// </summary>
/// <param name="sender"></param>
/// <param name="name"></param>
/// <returns></returns>
public static string PropertyGet(this object sender, string name)
/// <param name="sender">대상 객체</param>
/// <param name="name">속성 이름</param>
/// <returns>속성 값</returns>
public static object PropertyGet(this object sender, string name)
{
PropertyInfo property;
GetCachedProperties(sender.GetType()).TryGetValue(name, out property);
return property?.GetValue(sender);
}
#endregion
#region [ ToClass() ] -------------------------------------------------
/// <summary>
/// DataTable을 제네릭 객체 리스트로 변환합니다.
/// </summary>
/// <typeparam name="T">제네릭 객체 타입</typeparam>
/// <param name="dataTable">원본 DataTable</param>
/// <returns>제네릭 객체 배열</returns>
public static T[] ToClass<T>(this DataTable dataTable) where T : class, new()
{
var properties = GetCachedProperties(typeof(T));
var list = new List<T>();
foreach (DataRow row in dataTable.Rows)
{
var obj = new T();
foreach (var prop in properties)
{
if (!dataTable.Columns.Contains(prop.Key) || row[prop.Key] is DBNull) continue;
var value = Convert.ChangeType(row[prop.Key], prop.Value.PropertyType);
prop.Value.SetValue(obj, value);
}
list.Add(obj);
}
return list.ToArray();
}
/// <param name="srcArray">원본 객체 배열</param>
/// <returns>제네릭 객체 배열</returns>
public static T[] ToClass<T>(this object[] srcArray) where T : class, new()
{
try
{
PropertyInfo property = sender.GetType().GetProperty(name);
if (property == null)
List<T> list = new List<T>();
var desProperties = GetCachedProperties(typeof(T));
foreach (object src in srcArray)
{
foreach (PropertyInfo item in sender.GetType().GetProperties())
T des = new T();
list.Add(des);
var srcProperties = GetCachedProperties(src.GetType());
foreach (var srcProperty in srcProperties)
{
if (string.Compare(item.Name, name, true) == 0)
try
{
property = item;
break;
if (!desProperties.TryGetValue(srcProperty.Key, out var desProperty)) continue;
var srcValue = srcProperty.Value.GetValue(src);
if (srcProperty.Value.PropertyType != desProperty.PropertyType)
{
srcValue = Convert.ChangeType(srcValue, desProperty.PropertyType);
}
desProperty.SetValue(des, srcValue);
}
catch (Exception ex)
{
XLogger.Instance.Warn(ex);
}
}
}
if (property == null) return string.Empty;
return property.GetValue(sender)?.ToString();
}
catch (Exception ex)
{
XLogger.Instance.Fatal(ex);
return string.Empty;
return list.ToArray();
}
catch { throw; }
}
/// <param name="src">DataTable</param>
/// <returns>List with generic objects</returns>
public static T ToClass<T>(this object src) where T : class, new()
{
try
{
T des = new T();
var desProperties = GetCachedProperties(typeof(T));
var srcProperties = GetCachedProperties(src.GetType());
foreach (var srcProperty in srcProperties)
{
try
{
if (!desProperties.TryGetValue(srcProperty.Key, out var desProperty)) continue;
var srcValue = srcProperty.Value.GetValue(src);
if (srcProperty.Value.PropertyType != desProperty.PropertyType)
{
srcValue = Convert.ChangeType(srcValue, desProperty.PropertyType);
}
desProperty.SetValue(des, srcValue);
}
catch (Exception ex)
{
XLogger.Instance.Warn(ex);
}
}
return des;
}
catch { throw; }
}
#endregion
/// <summary>
/// 객체의 메소드를 호출합니다.
/// </summary>
@@ -352,141 +453,6 @@ namespace JWH
return null;
}
#region [ ToClass() ] -------------------------------------------------
/// <summary>
/// Converts a DataTable to a list with generic objects
/// dataTable.ToClass<Employee>();
/// </summary>
/// <typeparam name="T">Generic object</typeparam>
/// <param name="dataTable">DataTable</param>
/// <returns>List with generic objects</returns>
public static T[] ToClass<T>(this DataTable dataTable) where T : class, new()
{
try
{
List<T> list = new List<T>();
foreach (var row in dataTable.AsEnumerable())
{
T obj = new T();
foreach (PropertyInfo property in obj.GetType().GetProperties())
{
try
{
if (dataTable.Columns.Contains(property.Name) == false) continue;
if (row[property.Name] is System.DBNull) continue;
var val = row[property.Name];
//if (property.PropertyType == typeof(DateTime) && val.GetType() == typeof(string))
// DateTime.TryParse(val.ToString(), out val);
property.SetValue(obj, Convert.ChangeType(val, property.PropertyType, CultureInfo.CurrentCulture), null);
}
catch
{
continue;
}
}
list.Add(obj);
}
return list.ToArray();
}
catch { throw; }
}
/// <summary>
/// Converts a object to a list with generic objects
/// dataTable.ToClass<Employee>();
/// </summary>
/// <typeparam name="T">Generic object</typeparam>
/// <param name="src">DataTable</param>
/// <returns>List with generic objects</returns>
public static T ToClass<T>(this object src) where T : class, new()
{
try
{
T des = new T();
Type desType = des.GetType();
Type srcType = src.GetType();
foreach (PropertyInfo srcProperty in srcType.GetProperties())
{
try
{
PropertyInfo desProperty = desType.GetProperty(srcProperty.Name);
if (desProperty == null) continue;
var srcValue = srcProperty.GetValue(src);
if (srcProperty.PropertyType != desProperty.PropertyType)
{
srcValue = Convert.ChangeType(srcValue, desProperty.PropertyType);
}
desProperty.SetValue(des, srcValue);
}
catch (Exception ex)
{
XLogger.Instance.Warn(ex);
}
}
return des;
}
catch { throw; }
}
/// <summary>
/// Converts a object to a list with generic objects
/// dataTable.ToClass<Employee>();
/// </summary>
/// <typeparam name="T">Generic object</typeparam>
/// <param name="srcArray">DataTable</param>
/// <returns>List with generic objects</returns>
public static T[] ToClass<T>(this object[] srcArray) where T : class, new()
{
try
{
List<T> list = new List<T>();
foreach (object src in srcArray)
{
T des = new T();
list.Add(des);
Type desType = des.GetType();
Type srcType = src.GetType();
foreach (PropertyInfo srcProperty in srcType.GetProperties())
{
try
{
PropertyInfo desProperty = desType.GetProperty(srcProperty.Name);
if (desProperty == null) continue;
var srcValue = srcProperty.GetValue(src);
if (srcProperty.PropertyType != desProperty.PropertyType)
{
srcValue = Convert.ChangeType(srcValue, desProperty.PropertyType);
}
desProperty.SetValue(des, srcValue);
}
catch (Exception ex)
{
XLogger.Instance.Warn(ex);
}
}
}
return list.ToArray();
}
catch { throw; }
}
#endregion
public static CultureInfo CultureInfo { get; set; }
public static string ToTitleCase(this string sender)