Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding does not have a Clone method, whats an effective way to copy it

I wish to copy a binding, this is so i can set a different source property on it without affecting the original binding. Is this just a case of setting all of the properties on the new binding to be the same as the old?

like image 891
Aran Mulholland Avatar asked Dec 16 '09 00:12

Aran Mulholland


2 Answers

I just noticed in BindingBase decompiled code that it has an internal Clone() method, so another (unsafe, don't try at home, use at your own risk, etc.) solution would be to use reflection to bypass the compiler's access limitations:

public static BindingBase CloneBinding(BindingBase bindingBase, BindingMode mode = BindingMode.Default)
{
    var cloneMethodInfo = typeof(BindingBase).GetMethod("Clone", BindingFlags.Instance | BindingFlags.NonPublic);
    return (BindingBase) cloneMethodInfo.Invoke(bindingBase, new object[] { mode });
}

Didn't try it, though, so it might not work.

like image 67
splintor Avatar answered Sep 22 '22 20:09

splintor


if you can't find a method to do this already create an exetension for Binding.

    public static class BindingExtensions
{
    public static Binding Clone(this Binding binding)
    {
        var cloned = new Binding();
        //copy properties here
        return cloned;
    }
}

public void doWork()
{
    Binding b= new Binding();
    Binding nb = b.Clone(); 
}
like image 30
gingerbreadboy Avatar answered Sep 21 '22 20:09

gingerbreadboy