나는 동일한 문제에 직면 해있다. 그리고 제 경우에는 해결책이 필요했습니다. DateTime
필드가있는 너무 복잡한 모델이 너무 많기 때문입니다.
가 는
public static class ObjectExtensions
{
/// <summary>
/// Convert all DateTime fields in a complex object from UTC to a destination time zone.
/// </summary>
/// <typeparam name="TInput">Type of an object that will be converted.</typeparam>
/// <param name="obj">Object that will be deeply converted.</param>
/// <param name="destTimeZone"><c>TimeZoneInfo</c> object of a destination time zone.</param>
public static void DeepConvertFromUtc<TInput>(this TInput obj, TimeZoneInfo destTimeZone)
where TInput : class
{
obj.DeepConvert(TimeZoneInfo.Utc, destTimeZone);
}
/// <summary>
/// Convert all DateTime fields in a complex object from source time zone to UTC.
/// </summary>
/// <typeparam name="TInput">Type of an object that will be converted.</typeparam>
/// <param name="obj">Object that will be deeply converted.</param>
/// <param name="sourceTimeZone"><c>TimeZoneInfo</c> object of a source time zone.</param>
public static void DeepConvertToUtc<TInput>(this TInput obj, TimeZoneInfo sourceTimeZone)
where TInput : class
{
obj.DeepConvert(sourceTimeZone, TimeZoneInfo.Utc);
}
/// <summary>
/// Convert all DateTime fields in a complex object from UTC to a destination time zone.
/// </summary>
/// <typeparam name="TInput">Type of an object that will be converted.</typeparam>
/// <param name="obj">Object that will be deeply converted.</param>
/// <param name="sourceTimeZone"><c>TimeZoneInfo</c> object of a source time zone.</param>
/// <param name="destTimeZone"><c>TimeZoneInfo</c> object of a destination time zone.</param>
public static void DeepConvertTime<TInput>(this TInput obj, TimeZoneInfo sourceTimeZone, TimeZoneInfo destTimeZone)
where TInput : class
{
obj.DeepConvert(sourceTimeZone, destTimeZone);
}
private static void DeepConvert<TInput>(this TInput obj, TimeZoneInfo sourceTimeZone, TimeZoneInfo destTimeZone)
where TInput : class
{
if (obj == null)
{
return;
}
var items = obj as ICollection;
if (items != null)
{
foreach (var item in items)
{
item.DeepConvert(sourceTimeZone, destTimeZone);
}
return;
}
var props = obj.GetType().GetProperties();
foreach (var prop in props.Where(prop => !IsIgnore(prop)))
{
if (prop.PropertyType == typeof(DateTime) || prop.PropertyType == typeof(DateTime?))
{
prop.ConvertDateTime(obj, sourceTimeZone, destTimeZone);
continue;
}
var value = prop.GetValue(obj);
var list = value as ICollection;
if (list != null)
{
foreach (var item in list)
{
item.DeepConvert(sourceTimeZone, destTimeZone);
}
continue;
}
// here I check that an object is located in one of my assemblies
if (prop.PropertyType.Assembly.FullName.StartsWith("Should be your namespace"))
{
value.DeepConvert(sourceTimeZone, destTimeZone);
}
}
}
private static void ConvertDateTime<TInput>(this PropertyInfo prop, TInput obj, TimeZoneInfo sourceTimeZone, TimeZoneInfo destTimeZone)
where TInput : class
{
var value = prop.GetValue(obj);
if (value != null)
{
var dateTime = DateTime.SpecifyKind((DateTime)value, DateTimeKind.Unspecified);
value = TimeZoneInfo.ConvertTime(dateTime, sourceTimeZone, destTimeZone);
var setMethod = prop.SetMethod;
if (setMethod != null)
{
setMethod.Invoke(obj, new[] { value });
}
}
}
private static bool IsIgnore(this PropertyInfo prop)
{
var attr = prop.GetCustomAttribute<IgnoreUtcConversionAttribute>();
return attr != null;
}
}
public class IgnoreUtcConversionAttribute : Attribute
{
}
이
당신은 아주 간단하게 사용할 수 있습니다 :
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Belarus Standard Time");
yourComplexObject.DeepConvertToUtc(timeZoneInfo);
당신 경우 :
yourComplexObject.DeepConvertToUtc(TimeZoneInfo.Local);
// a collection could be converted as well
collection.DeepConvertToUtc(TimeZoneInfo.Local);
은 또한 당신이 FindSystemTimeZoneById
방법에서 TimeZineInfo
개체를 얻을 수 있습니다 내 요구를 들어 나는 다음 확장을 썼다 모델에 양방향 효과가 있으면 [IgnoreUtcConversion]
:
을 사용하여 한 가지 설명을 장식해야합니다.
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime Birth { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string Street { get; set; }
public DateTime LastUpdated { get; set; }
[IgnoreUtcConversion]
public Customer Customer { get; set; }
}
시도한 코드는 어떻게됩니까? 어떤 DAL을 사용하고 있습니까? EF 또는 ADO.NET? –