91 lines
2.7 KiB
C#
91 lines
2.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Web;
|
|
|
|
namespace OnDocAPI_NetFramework.Helper
|
|
{
|
|
public static class Helper
|
|
{
|
|
public static void CopyProperties(object source, object target)
|
|
{
|
|
var sourceProps = source.GetType().GetProperties();
|
|
var targetProps = target.GetType().GetProperties();
|
|
|
|
foreach (var prop in sourceProps)
|
|
{
|
|
var targetProp = targetProps.FirstOrDefault(p => p.Name == prop.Name && p.PropertyType == prop.PropertyType);
|
|
if (targetProp != null && targetProp.CanWrite)
|
|
{
|
|
targetProp.SetValue(target, prop.GetValue(source));
|
|
}
|
|
}
|
|
}
|
|
|
|
private static readonly Random _random = new Random();
|
|
|
|
public static string RandomString(int size, bool lowerCase = false)
|
|
{
|
|
var builder = new StringBuilder(size);
|
|
|
|
char offset = lowerCase ? 'a' : 'A';
|
|
const int lettersOffset = 26; // A...Z or a..z: length=26
|
|
|
|
for (var i = 0; i < size; i++)
|
|
{
|
|
var @char = (char)_random.Next(offset, offset + lettersOffset);
|
|
builder.Append(@char);
|
|
}
|
|
|
|
return lowerCase ? builder.ToString().ToLower() : builder.ToString();
|
|
}
|
|
|
|
public static string NormalizeDateToSystemFormat(string input)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(input))
|
|
return input;
|
|
|
|
var culture = CultureInfo.CurrentCulture;
|
|
|
|
string[] knownFormats =
|
|
{
|
|
"yyyy-MM-dd",
|
|
"yyyyMMdd",
|
|
"dd.MM.yyyy",
|
|
"d.M.yyyy",
|
|
"dd/MM/yyyy",
|
|
"d/MM/yyyy",
|
|
"d/M/yyyy",
|
|
"MM/dd/yyyy",
|
|
"yyyy-MM-ddTHH:mm:ss",
|
|
"yyyy-MM-dd HH:mm:ss",
|
|
"dd.MM.yyyy HH:mm:ss",
|
|
"d.M.yyyy HH:mm:ss"
|
|
};
|
|
|
|
DateTime date;
|
|
|
|
// 1. Exakte Formate
|
|
if (DateTime.TryParseExact(
|
|
input.Trim(),
|
|
knownFormats,
|
|
culture,
|
|
DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.AssumeLocal,
|
|
out date))
|
|
{
|
|
return date.ToString(culture.DateTimeFormat.ShortDatePattern, culture);
|
|
}
|
|
|
|
// 2. Fallback: freie Erkennung
|
|
if (DateTime.TryParse(input, culture, DateTimeStyles.AssumeLocal, out date))
|
|
{
|
|
return date.ToString(culture.DateTimeFormat.ShortDatePattern, culture);
|
|
}
|
|
|
|
return input;
|
|
}
|
|
}
|
|
|
|
} |