forked from neo-project/neo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve p2p message deserialization (neo-project#1262)
- Loading branch information
Showing
6 changed files
with
71 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace Neo.IO.Caching | ||
{ | ||
public class ReflectionCache<T> : Dictionary<T, Type> | ||
internal static class ReflectionCache<T> where T : Enum | ||
{ | ||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
public ReflectionCache() { } | ||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
/// <typeparam name="EnumType">Enum type</typeparam> | ||
public static ReflectionCache<T> CreateFromEnum<EnumType>() where EnumType : struct, IConvertible | ||
{ | ||
Type enumType = typeof(EnumType); | ||
|
||
if (!enumType.GetTypeInfo().IsEnum) | ||
throw new ArgumentException("K must be an enumerated type"); | ||
private static readonly Dictionary<T, Type> dictionary = new Dictionary<T, Type>(); | ||
|
||
// Cache all types | ||
ReflectionCache<T> r = new ReflectionCache<T>(); | ||
public static int Count => dictionary.Count; | ||
|
||
foreach (object t in Enum.GetValues(enumType)) | ||
static ReflectionCache() | ||
{ | ||
Type enumType = typeof(T); | ||
foreach (FieldInfo field in enumType.GetFields(BindingFlags.Public | BindingFlags.Static)) | ||
{ | ||
// Get enumn member | ||
MemberInfo[] memInfo = enumType.GetMember(t.ToString()); | ||
if (memInfo == null || memInfo.Length != 1) | ||
throw (new FormatException()); | ||
|
||
// Get attribute | ||
ReflectionCacheAttribute attribute = memInfo[0].GetCustomAttributes(typeof(ReflectionCacheAttribute), false) | ||
.Cast<ReflectionCacheAttribute>() | ||
.FirstOrDefault(); | ||
|
||
if (attribute == null) | ||
throw (new FormatException()); | ||
ReflectionCacheAttribute attribute = field.GetCustomAttribute<ReflectionCacheAttribute>(); | ||
if (attribute == null) continue; | ||
|
||
// Append to cache | ||
r.Add((T)t, attribute.Type); | ||
dictionary.Add((T)field.GetValue(null), attribute.Type); | ||
} | ||
return r; | ||
} | ||
/// <summary> | ||
/// Create object from key | ||
/// </summary> | ||
/// <param name="key">Key</param> | ||
/// <param name="def">Default value</param> | ||
public object CreateInstance(T key, object def = null) | ||
{ | ||
Type tp; | ||
|
||
public static object CreateInstance(T key, object def = null) | ||
{ | ||
// Get Type from cache | ||
if (TryGetValue(key, out tp)) return Activator.CreateInstance(tp); | ||
if (dictionary.TryGetValue(key, out Type t)) | ||
return Activator.CreateInstance(t); | ||
|
||
// return null | ||
return def; | ||
} | ||
/// <summary> | ||
/// Create object from key | ||
/// </summary> | ||
/// <typeparam name="K">Type</typeparam> | ||
/// <param name="key">Key</param> | ||
/// <param name="def">Default value</param> | ||
public K CreateInstance<K>(T key, K def = default(K)) | ||
{ | ||
Type tp; | ||
|
||
// Get Type from cache | ||
if (TryGetValue(key, out tp)) return (K)Activator.CreateInstance(tp); | ||
|
||
// return null | ||
return def; | ||
public static ISerializable CreateSerializable(T key, byte[] data) | ||
{ | ||
if (dictionary.TryGetValue(key, out Type t)) | ||
return data.AsSerializable(t); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters