Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to ignore Events or Properties #10

Open
FutureTD opened this issue Feb 26, 2019 · 9 comments
Open

Ability to ignore Events or Properties #10

FutureTD opened this issue Feb 26, 2019 · 9 comments

Comments

@FutureTD
Copy link

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 FutureTD changed the title Ability to ignore Events / Properties Ability to ignore Events or Properties Feb 26, 2019
@force-net
Copy link
Owner

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

@FutureTD
Copy link
Author

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.

@force-net
Copy link
Owner

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

@luislhg
Copy link

luislhg commented Mar 5, 2020

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!

@maskalek
Copy link

maskalek commented May 4, 2020

Any hacks? How to override behavior?

@force-net
Copy link
Owner

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 :)

@GF-Huang
Copy link

Any progress?

@sgf
Copy link

sgf commented Dec 6, 2023

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

@Dunge
Copy link

Dunge commented Sep 17, 2024

Adding the event in the the ignore code above doesn't seem to be working.

    public class Test : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string _prop;
        public string Prop
        {
            get
            {
                return _prop;
            }

            set
            {
                _prop = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Prop)));
            }
        }
    }

    public void TestMethod()
    {
            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(PropertyChangedEventHandler), true);

            var a = new Test();
            a.PropertyChanged += (sender, args) => { _logger.LogInformation(((Test)sender).Prop); };
            a.Prop = "A changed";
            var b = a.DeepClone();
            b.Prop = "B changed";
    }

The event trigger on the cloned objects and it prints both changed line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants