mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 05:05:05 +00:00
Util method to cache a Type for JSON serialization
This commit is contained in:
@@ -29,8 +29,16 @@ namespace ICD.Common.Utils.Json
|
|||||||
public static void CacheType<T>()
|
public static void CacheType<T>()
|
||||||
where T : new()
|
where T : new()
|
||||||
{
|
{
|
||||||
string serialized = JsonConvert.SerializeObject(ReflectionUtils.CreateInstance<T>());
|
CacheType(typeof(T));
|
||||||
JsonConvert.DeserializeObject<T>(serialized);
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Forces Newtonsoft to cache the given type for faster subsequent usage.
|
||||||
|
/// </summary>
|
||||||
|
public static void CacheType(Type type)
|
||||||
|
{
|
||||||
|
string serialized = JsonConvert.SerializeObject(ReflectionUtils.CreateInstance(type));
|
||||||
|
JsonConvert.DeserializeObject(serialized, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -210,6 +210,26 @@ namespace ICD.Common.Utils
|
|||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true if the given type has a public parameterless constructor.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="type"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool HasPublicParameterlessConstructor(Type type)
|
||||||
|
{
|
||||||
|
if (type == null)
|
||||||
|
throw new ArgumentNullException("type");
|
||||||
|
|
||||||
|
const BindingFlags binding = BindingFlags.Instance | BindingFlags.Public;
|
||||||
|
|
||||||
|
#if SIMPLSHARP
|
||||||
|
return ((CType)type).GetConstructor(binding, null, new CType[0], null)
|
||||||
|
#else
|
||||||
|
return type.GetConstructor(binding, null, new Type[0], null)
|
||||||
|
#endif
|
||||||
|
!= null;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates an instance of the given type, calling the default constructor.
|
/// Creates an instance of the given type, calling the default constructor.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user