- Programmiersprache von VSC zu C# geändert - Framework von .NET Framework zu .NET geändert
64 lines
1.4 KiB
C#
64 lines
1.4 KiB
C#
using System.Data;
|
|
using DigitalData.Modules.Logging;
|
|
|
|
namespace DigitalData.Modules.Base;
|
|
|
|
public abstract class BaseClass
|
|
{
|
|
protected BaseClass(LogConfig logConfig)
|
|
{
|
|
LogConfig = logConfig;
|
|
Logger = logConfig.GetLogger();
|
|
}
|
|
|
|
protected LogConfig LogConfig { get; }
|
|
protected Logger Logger { get; }
|
|
}
|
|
|
|
public static class ObjectEx
|
|
{
|
|
public static T ToEnum<T>(object value) where T : struct, Enum
|
|
{
|
|
if (value is T enumValue)
|
|
{
|
|
return enumValue;
|
|
}
|
|
|
|
if (value is string stringValue && Enum.TryParse<T>(stringValue, true, out var parsed))
|
|
{
|
|
return parsed;
|
|
}
|
|
|
|
if (int.TryParse(Convert.ToString(value), out var intValue))
|
|
{
|
|
return (T)Enum.ToObject(typeof(T), intValue);
|
|
}
|
|
|
|
return default;
|
|
}
|
|
}
|
|
|
|
public static class DataRowExtensions
|
|
{
|
|
public static T ItemEx<T>(this DataRow row, string columnName, T defaultValue)
|
|
{
|
|
if (!row.Table.Columns.Contains(columnName))
|
|
{
|
|
return defaultValue;
|
|
}
|
|
|
|
var value = row[columnName];
|
|
if (value is DBNull or null)
|
|
{
|
|
return defaultValue;
|
|
}
|
|
|
|
return (T)Convert.ChangeType(value, typeof(T));
|
|
}
|
|
|
|
public static string ItemEx(this DataRow row, string columnName, string defaultValue)
|
|
{
|
|
return ItemEx<string>(row, columnName, defaultValue);
|
|
}
|
|
}
|