public void OverloadTest_WithoutObjects() { Script s = new Script(); // Create an instance of the overload resolver var ov = new OverloadedMethodMemberDescriptor("Method1", this.GetType()); // Iterate over the two methods through reflection foreach (var method in this.GetType().GetMethods(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance) .Where(mi => mi.Name == "Method1")) { ov.AddOverload(new MethodMemberDescriptor(method)); } // Creates the callback over the 'this' object DynValue callback = DynValue.NewCallback(ov.GetCallbackFunction(s, this)); s.Globals.Set("func", callback); // Execute and check the results. DynValue result = s.DoString("return func(), func(17)"); Assert.AreEqual(DataType.Tuple, result.Type); Assert.AreEqual(DataType.Number, result.Tuple[0].Type); Assert.AreEqual(DataType.Number, result.Tuple[1].Type); Assert.AreEqual(1, result.Tuple[0].Number); Assert.AreEqual(22, result.Tuple[1].Number); }
void AddOverloadedMethod(Script s, string name) { var ov = new OverloadedMethodMemberDescriptor(name, Type); foreach (var method in Type.GetMethods(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance) .Where(mi => mi.Name == name)) { ov.AddOverload(new MethodMemberDescriptor(method)); } vars.Add(DynValue.NewString(name), DynValue.NewCallback(ov.GetCallbackFunction(s))); }
private void AddMemberTo(Dictionary <string, IMemberDescriptor> members, string name, IMemberDescriptor desc) { IOverloadableMemberDescriptor odesc = desc as IOverloadableMemberDescriptor; if (odesc != null) { if (members.ContainsKey(name)) { OverloadedMethodMemberDescriptor overloads = members[name] as OverloadedMethodMemberDescriptor; if (overloads != null) { overloads.AddOverload(odesc); } else { throw new ArgumentException(string.Format("Multiple members named {0} are being added to type {1} and one or more of these members do not support overloads.", name, this.Type.FullName)); } } else { members.Add(name, new OverloadedMethodMemberDescriptor(name, this.Type, odesc)); } } else { if (members.ContainsKey(name)) { throw new ArgumentException(string.Format("Multiple members named {0} are being added to type {1} and one or more of these members do not support overloads.", name, this.Type.FullName)); } else { members.Add(name, desc); } } }
public void OverloadTest_WithoutObjects() { Script s = new Script(); // Create an instance of the overload resolver var ov = new OverloadedMethodMemberDescriptor("Method1", this.GetType()); // Iterate over the two methods through reflection foreach(var method in this.GetType().GetMethods(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance) .Where(mi => mi.Name == "Method1")) { ov.AddOverload(new MethodMemberDescriptor(method)); } // Creates the callback over the 'this' object DynValue callback = DynValue.NewCallback(ov.GetCallbackFunction(s, this)); s.Globals.Set("func", callback); // Execute and check the results. DynValue result = s.DoString("return func(), func(17)"); Assert.AreEqual(DataType.Tuple, result.Type); Assert.AreEqual(DataType.Number, result.Tuple[0].Type); Assert.AreEqual(DataType.Number, result.Tuple[1].Type); Assert.AreEqual(1, result.Tuple[0].Number); Assert.AreEqual(22, result.Tuple[1].Number); }