Skip to content

Commit f08e05a

Browse files
ChrisBardsleyamaitland
authored andcommitted
Fix for C# paramArray and refactor on arguments mismatch
1 parent dfa1480 commit f08e05a

File tree

7 files changed

+87
-5
lines changed

7 files changed

+87
-5
lines changed

CefSharp.Example/Resources/Home.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,14 @@ <h3 id="features-javascript-integration">JavaScript integration</h3>
351351
objects.
352352
</p>
353353
<h3>Hooks by which you can modify and/or override certain features of the web browsing</h3>
354+
<p>
355+
A fix to allow params to be used by the .NET methods called by the javascript<br />
356+
<button onclick="boundEvent.methodWithParams('test', 'hello-world')">With 1 Params</button>
357+
<button onclick="boundEvent.methodWithParams('test', 'hello-world', 'chris was here')">With 2 Params</button>
358+
<button onclick="boundEvent.methodWithParams('test')">With no Params</button>
359+
<button onclick="boundEvent.methodWithoutParams('test', 'hello')">Normal Method</button>
360+
<button onclick="boundEvent.methodWithoutAnything()">Normal Method No Params</button>
361+
</p>
354362
</div>
355363
<div class="bs-docs-section">
356364
<div class="page-header">

CefSharp.Example/ScriptedMethodsBoundObject.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//
33
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
44
using System;
5+
using System.Linq;
56

67
namespace CefSharp.Example
78
{
@@ -35,5 +36,26 @@ public void RaiseEvent(string eventName, object eventData = null)
3536
EventArrived(eventName, eventData);
3637
}
3738
}
39+
40+
/// <summary>
41+
/// Demonstrates the use of params as an argument in a bound object
42+
/// </summary>
43+
/// <param name="name">Dummy Argument</param>
44+
/// <param name="args">Params Argument</param>
45+
public void MethodWithParams(string name, params object[] args)
46+
{
47+
var test = args.ElementAtOrDefault(0);
48+
var test2 = args.ElementAtOrDefault(1);
49+
}
50+
51+
public void MethodWithoutParams(string name, string arg2)
52+
{
53+
var test = arg2;
54+
}
55+
56+
public void methodWithoutAnything()
57+
{
58+
var test = "";
59+
}
3860
}
3961
}

CefSharp/CefSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<Compile Include="IDomNode.cs" />
9090
<Compile Include="IFindHandler.cs" />
9191
<Compile Include="Internals\CommandLineArgsParser.cs" />
92+
<Compile Include="Internals\MethodParameter.cs" />
9293
<Compile Include="IPluginHandler.cs" />
9394
<Compile Include="IPopupFeatures.cs" />
9495
<Compile Include="IRenderProcessMessageHandler.cs" />

CefSharp/Internals/JavascriptMethod.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
44

55
using System;
6+
using System.Collections.Generic;
67
using System.Runtime.Serialization;
78

89
namespace CefSharp.Internals
@@ -33,6 +34,9 @@ public class JavascriptMethod
3334
[DataMember]
3435
public string JavascriptName { get; set; }
3536

37+
[DataMember]
38+
public List<MethodParameter> MethodParameters { get; set; }
39+
3640
/// <summary>
3741
/// Number of Params this function exepects
3842
/// </summary>

CefSharp/Internals/JavascriptObjectRepository.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,33 @@ public bool TryCallMethod(long objectId, string name, object[] parameters, out o
104104

105105
try
106106
{
107-
// Do we have enough arguments? Add Type.Missing for any that we don't have incase of optional params
107+
//Fix for C# paramArray and refactor on arguments mismatch 03/22/2016
108108
var missingParams = method.ParameterCount - parameters.Length;
109-
if (missingParams > 0)
109+
if (missingParams > 0 || method.MethodParameters.Any(t => t.IsParamArray))
110110
{
111-
var paramList = new List<object>(parameters);
111+
var paramList = new List<object>();
112112

113-
for (var i = 0; i < missingParams; i++)
113+
for (int i = 0; i < method.MethodParameters.Count; i++)
114114
{
115-
paramList.Add(Type.Missing);
115+
object jsParam = parameters.ElementAtOrDefault(i);
116+
if (jsParam == null && !method.MethodParameters[i].IsParamArray)
117+
{
118+
paramList.Add(Type.Missing);
119+
}
120+
else if (method.MethodParameters[i].IsParamArray)
121+
{
122+
List<object> convertedParams = new List<object>();
123+
for (int s = i; s < parameters.Count(); s++)
124+
{
125+
convertedParams.Add(parameters[s]);
126+
}
127+
paramList.Add(convertedParams.ToArray());
128+
break;
129+
}
130+
else
131+
{
132+
paramList.Add(jsParam);
133+
}
116134
}
117135

118136
parameters = paramList.ToArray();
@@ -287,6 +305,14 @@ private static JavascriptMethod CreateJavaScriptMethod(MethodInfo methodInfo, bo
287305
jsMethod.JavascriptName = GetJavascriptName(methodInfo.Name, camelCaseJavascriptNames);
288306
jsMethod.Function = methodInfo.Invoke;
289307
jsMethod.ParameterCount = methodInfo.GetParameters().Length;
308+
jsMethod.MethodParameters = methodInfo.GetParameters()
309+
.Select(t => new MethodParameter()
310+
{ Name = t.Name,
311+
ParameterType = t.ParameterType,
312+
Position = t.Position,
313+
IsParamArray = t.GetCustomAttributes(typeof(ParamArrayAttribute), false).Length > 0
314+
}).ToList();
315+
290316

291317
return jsMethod;
292318
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.Serialization;
5+
using System.Text;
6+
7+
namespace CefSharp.Internals
8+
{
9+
[DataContract]
10+
public class MethodParameter
11+
{
12+
public String Name { get; set; }
13+
14+
public Type ParameterType { get; set; }
15+
16+
public Boolean IsParamArray { get; set; }
17+
18+
public Int32 Position { get; set; }
19+
}
20+
}

NuGet.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
</activePackageSource>
66
<packageSources>
77
<add key="cefsharp-myget" value="https://www.myget.org/F/cefsharp/" />
8+
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
89
</packageSources>
910
</configuration>

0 commit comments

Comments
 (0)