DeepCloner icon indicating copy to clipboard operation
DeepCloner copied to clipboard

Ability to ignore Events or Properties

Open FutureTD opened this issue 6 years ago • 9 comments

Hello, I have quite a few classes that implement INotifyPropertyChanged due to mvvm, cloning these classes seems to lead to a memory error and crash problem.

Is there a way to not clone events or specific properties?

FutureTD avatar Feb 26 '19 00:02 FutureTD

Currently, there is no such ability. I've received another similar issues and will try to implement related feature.

force-net avatar Feb 26 '19 05:02 force-net

Has this been implemented yet? This is a great library but this causes problems if a object implements INotifyPropertyChanged.

Maybe you can hard code it to not clone that property for the time being.

FutureTD avatar Mar 19 '19 13:03 FutureTD

Not implemented yet. Sorry, I have no time to do this in nearest time. It not simple because code tries to clone object itself in one step (if possible), for this case I need some more analyzing to find best way to copy and test it. Also, before implementing I should check, whereas real problem exists. I'll to to implement this asap, but cannot give any estimations about time. Sorry

force-net avatar Mar 19 '19 15:03 force-net

Same issue here... When using INotifyPropertyChanged it becomes a problem that it copies the events. I was using MemberwiseClone before and had the same issue... Hoping for this to be configurable anytime soon. Thanks!

luislhg avatar Mar 05 '20 05:03 luislhg

Any hacks? How to override behavior?

maskalek avatar May 04 '20 08:05 maskalek

I have ideas, but do not have enough time to fix it. You can try to do something like

var assembly = Assembly.GetAssembly(typeof(DeepClonerExtensions));
var deepClonerSafeTypes = assembly.GetType("Force.DeepCloner.Helpers.DeepClonerSafeTypes");
var knownTypesField = deepClonerSafeTypes.GetField("KnownTypes", BindingFlags.Static | BindingFlags.NonPublic);
var knownTypes = (ConcurrentDictionary<Type, bool>)knownTypesField.GetValue(null);
knownTypes.TryAdd(typeof(type_to_ignore), true);

It's dirty hack, and it will not work in future versions, but I'll try to do something with this feature in future versions :)

force-net avatar May 06 '20 18:05 force-net

Any progress?

GF-Huang avatar Sep 24 '21 12:09 GF-Huang

A feasible method may be to add a generic overload to DeepClone DeepClone<T>(params Expression<T,TFieldName>[] ignoreExps) or DeepClone<T>(params string[] ignoreExps) Get the field names, attribute names, etc. that need to be ignored through ignoreExp.

sgf avatar Dec 06 '23 03:12 sgf