public void DelegateGroupTest() { Tester t = new Tester(); // overloaded instance method with a target DelegateGroup dg = (DelegateGroup)Invoker.InvokeMember(typeof(Tester), "Two", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, t, null); Assert.Equal(2, (int)dg.DynamicInvoke()); // assign the group to a property (this will pick one method out of the group) Invoker.InvokeMember(typeof(Tester), "delegateProperty", BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty, t, dg); Assert.Equal(2, (int) Invoker.InvokeMember(typeof(Tester), "delegateProperty", BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, t, new object[0])); // instance method with no target dg = (DelegateGroup)Invoker.InvokeMember(typeof(Tester), "One", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, null); Assert.Equal(1, (int)dg.DynamicInvoke(t)); Converter <Tester, int> cti = (Converter <Tester, int>)dg.GetDelegate(typeof(Converter <Tester, int>)); Assert.Equal(1, cti(t)); // overloaded instance method with no target dg = (DelegateGroup)Invoker.InvokeMember(typeof(Tester), "Two", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, null); Assert.Equal(2, (int)dg.DynamicInvoke(t)); cti = (Converter <Tester, int>)dg.GetDelegate(typeof(Converter <Tester, int>)); Assert.Equal(2, cti(t)); // static method with no target dg = (DelegateGroup)Invoker.InvokeMember(typeof(Tester), "Three", BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod, null, null); Assert.Equal(3, (int)dg.DynamicInvoke()); }
public static Delegate InstanceRemove(Delegate source, Delegate value) { if (RpcHead.Current == null)//client { return(Delegate.Remove(source, value)); } Guid instanceId = RpcHead.Current.InstanceId; DelegateGroup group = null; if (!delegates.ContainsKey(instanceId)) { group = new DelegateGroup(); delegates.Add(instanceId, group); } else { group = delegates[instanceId]; } Delegate fixedSource = null; if (!group.ContainsKey(value.Method)) { if (source != null) { group.Add(value.Method, source); fixedSource = source; } } else { fixedSource = group[value.Method]; } var retVal = Delegate.Remove(fixedSource, value); if (retVal == null) { group.Remove(value.Method); } else { group[value.Method] = retVal; } return(retVal); }