sections + start collections

This commit is contained in:
2021-11-23 11:35:01 +01:00
parent d4e74fac6c
commit a0cbd4695c
43 changed files with 920 additions and 1348 deletions
+23 -41
View File
@@ -1,48 +1,30 @@
using Newtonsoft.Json;
//using Newtonsoft.Json;
namespace zero.Extensions;
//namespace zero.Extensions;
[Obsolete("we don't want this for every object (use a Utils class instead)")]
public static class ObjectExtensions
{
public static bool Is<T>(this Type type)
{
return type.IsAssignableFrom(typeof(T));
}
//[Obsolete("we don't want this for every object (use a Utils class instead)")]
//public static class ObjectExtensions
//{
// public static bool Is<T>(this Type type)
// {
// return type.IsAssignableFrom(typeof(T));
// }
public static bool Is<T>(this object obj)
{
return obj.GetType().IsAssignableFrom(typeof(T));
}
// public static bool Is<T>(this object obj)
// {
// return obj.GetType().IsAssignableFrom(typeof(T));
// }
public static T Clone<T>(this T obj)
{
Type type = obj.GetType();
return (T)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(obj, new RefJsonConverter()), type, new RefJsonConverter());
}
// public static T Clone<T>(this T obj)
// {
// Type type = obj.GetType();
// return (T)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(obj), type);
// }
public static T CloneLax<T>(this object obj)
{
return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(obj, new RefJsonConverter()), new RefJsonConverter());
}
public static T AutoSetIds<T>(this T obj)
{
// find all Raven Ids
List<ObjectTraverser.Result<GenerateIdAttribute>> ravenIds = ObjectTraverser.FindAttribute<GenerateIdAttribute>(obj);
// set unset Raven Ids
foreach (ObjectTraverser.Result<GenerateIdAttribute> 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;
}
}
// public static T CloneLax<T>(this object obj)
// {
// return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(obj));
// }
//}