Skip to content

Commit 16df485

Browse files
committed
Split paramArray and missing arg logic out into two if statements. Previously null was being replaced by Type.Missing.
Two seperate if statement branches allows for a method that has options params and a param array Resolve #1673
1 parent 5c20c7e commit 16df485

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

CefSharp.Example/BoundObject.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,5 +291,10 @@ public string MethodWithoutAnything()
291291
{
292292
return "Method without anything called and returned successfully.";
293293
}
294+
295+
public string MethodWithThreeParamsOneOptionalOneArray(string name, string optionalParam = null, params object[] args)
296+
{
297+
return "MethodWithThreeParamsOneOptionalOneArray:" + (name ?? "No Name Specified") + " - " + (optionalParam ?? "No Optional Param Specified") + ";Args:" + string.Join(", ", args.ToArray());
298+
}
294299
}
295300
}

CefSharp.Example/Resources/BindingTest.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@
245245
document.write(bound.methodWithParams('With no Params') + "<br/>");
246246
document.write(bound.methodWithoutParams('Normal Method', 'hello') + "<br/>");
247247
document.write(bound.methodWithoutAnything() + "<br/>");
248+
249+
document.write(bound.methodWithThreeParamsOneOptionalOneArray(null) + "<br/>");
250+
document.write(bound.methodWithThreeParamsOneOptionalOneArray(null, null) + "<br/>");
251+
document.write(bound.methodWithThreeParamsOneOptionalOneArray("Test", null) + "<br/>");
252+
document.write(bound.methodWithThreeParamsOneOptionalOneArray(null, null, "Arg1", "Arg2") + "<br/>");
248253
</script>
249254
</p>
250255
</body>

CefSharp/Internals/JavascriptObjectRepository.cs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,30 +104,21 @@ public bool TryCallMethod(long objectId, string name, object[] parameters, out o
104104

105105
try
106106
{
107-
//Added param array support #1644
108-
var missingParams = method.ParameterCount - parameters.Length;
109-
//CONDITION #1 : Check for parameter count missmatch between the parameters on the javascript function and the
110-
// number of parameters on the bound object method. (This is relevant for methods that have default values)
111-
//CONDITION #2 : Check if the bound object method contains a ParamArray as the last parameter on the method signature.
112-
if (missingParams > 0 || method.HasParamArray)
107+
//Check if the bound object method contains a ParamArray as the last parameter on the method signature.
108+
//NOTE: No additional parameters are permitted after the params keyword in a method declaration,
109+
//and only one params keyword is permitted in a method declaration.
110+
//https://msdn.microsoft.com/en-AU/library/w5zay9db.aspx
111+
if (method.HasParamArray)
113112
{
114113
var paramList = new List<object>(method.Parameters.Count);
115114

116115
//Loop through all of the method parameters on the bound object.
117116
for (var i = 0; i < method.Parameters.Count; i++)
118117
{
119-
//Attempt to get the javascript function param at the current bound object parameter index.
120-
//If the javascript function param is missing IE: NULL, then add Type.Missing.
121-
//This will allow for default bound object parameters. IE: (string someParameter = "someValue")
122-
var jsParam = parameters.ElementAtOrDefault(i);
123-
if (jsParam == null && !method.Parameters[i].IsParamArray)
124-
{
125-
paramList.Add(Type.Missing);
126-
}
127-
//If the method parameter is a paramArray IE: (params Object[] someParameter)
118+
//If the method parameter is a paramArray IE: (params string[] args)
128119
//grab the parameters from the javascript function starting at the current bound object parameter index
129120
//and add create an array that will be passed in as the last bound object method parameter.
130-
else if (method.Parameters[i].IsParamArray)
121+
if (method.Parameters[i].IsParamArray)
131122
{
132123
var convertedParams = new List<object>();
133124
for (var s = i; s < parameters.Length; s++)
@@ -138,12 +129,30 @@ public bool TryCallMethod(long objectId, string name, object[] parameters, out o
138129
}
139130
else
140131
{
132+
var jsParam = parameters.ElementAtOrDefault(i);
141133
paramList.Add(jsParam);
142134
}
143135
}
144136

145137
parameters = paramList.ToArray();
146138
}
139+
140+
//Check for parameter count missmatch between the parameters on the javascript function and the
141+
//number of parameters on the bound object method. (This is relevant for methods that have default values)
142+
//NOTE it's possible to have default params and a paramArray, so check missing params last
143+
var missingParams = method.ParameterCount - parameters.Length;
144+
145+
if(missingParams > 0)
146+
{
147+
var paramList = new List<object>(parameters);
148+
149+
for (var i = 0; i < missingParams; i++)
150+
{
151+
paramList.Add(Type.Missing);
152+
}
153+
154+
parameters = paramList.ToArray();
155+
}
147156

148157
try
149158
{

0 commit comments

Comments
 (0)