-
Notifications
You must be signed in to change notification settings - Fork 165
Closed
Description
InMemoryRepository will not recognise that some properties cannot be written to and will always treat every property as being serializable. Having a property that should not be serialized (for example, see the following) will cause the InMemoryRepository to throw an exception when it tries to deep-clone the repository upon calls to GetAll()
.....
public DateTime UnbanTime { get; set; }
public bool Expired { get { return DateTime.Now >= UnbanTime; } }I suggest the use of skipping properties without setters, or allowing users to specify properties as [NonSerializable] so that InMemoryRepository won't attempt to serialize it.
// Inside InMemoryRepositoryBase<T>
private static IEnumerable<T> CloneDictionary(ConcurrentDictionary<TKey, T> list)
{
// when you Google deep copy of generic list every answer uses either the IClonable interface on the T or having the T be Serializable
// since we can't really put those constraints on T I'm going to do it via reflection
var type = typeof (T);
var properties = type.GetProperties();
var clonedList = new List<T>(list.Count);
foreach (var keyValuePair in list)
{
var newItem = new T();
foreach (var propInfo in properties) // <- here is where the issue lies
{
// v - will break if the property does not have a setter
propInfo.SetValue(newItem, propInfo.GetValue(keyValuePair.Value, null), null);
}
clonedList.Add(newItem);
}
return clonedList;
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels