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

前端 未结 4 1206
情歌与酒
情歌与酒 2020-12-11 03:03

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

4条回答
  •  鱼传尺愫
    2020-12-11 03:39

    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.

提交回复
热议问题