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?
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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With