using Newtonsoft.Json; using System; using System.Collections.Generic; using zero.Core.Attributes; using zero.Core.Utils; namespace zero.Core.Extensions { public static class ObjectExtensions { public static bool Is(this Type type) { return type.IsAssignableFrom(typeof(T)); } public static bool Is(this object obj) { return obj.GetType().IsAssignableFrom(typeof(T)); } public static T Clone(this T obj) { Type type = obj.GetType(); return (T)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(obj, new RefJsonConverter()), type, new RefJsonConverter()); } public static T CloneLax(this object obj) { return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(obj, new RefJsonConverter()), new RefJsonConverter()); } public static T AutoSetIds(this T obj) { // find all Raven Ids List> ravenIds = ObjectTraverser.FindAttribute(obj); // set unset Raven Ids foreach (ObjectTraverser.Result item in ravenIds) { string id = item.Property.GetValue(item.Parent, null) as string; if (String.IsNullOrWhiteSpace(id)) { item.Property.SetValue(item.Parent, item.Item.Length.HasValue ? IdGenerator.Create(item.Item.Length.Value) : IdGenerator.Create()); } } return obj; } } }