Refactored changes from build#1644 to account for discussed changes.

This commit is contained in:
Chris Bardsley
2016-04-04 21:44:22 -07:00
parent b628199ee9
commit d3d6907dac
8 changed files with 73 additions and 51 deletions
+21
View File
@@ -3,6 +3,7 @@
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Linq;
using System.Threading.Tasks;
namespace CefSharp.Example
@@ -270,5 +271,25 @@ namespace CefSharp.Example
{
return SubObject;
}
/// <summary>
/// Demonstrates the use of params as an argument in a bound object
/// </summary>
/// <param name="name">Dummy Argument</param>
/// <param name="args">Params Argument</param>
public string MethodWithParams(string name, params object[] args)
{
return String.Join(", ", args.ToArray());
}
public string MethodWithoutParams(string name, string arg2)
{
return String.Format("{0}, {1}", name, arg2);
}
public string methodWithoutAnything()
{
return "Method without anything called and returned successfully.";
}
}
}
@@ -237,5 +237,34 @@
}
</script>
</ul>
<p>
Javascript function sending variable number of parameters to Bound Object. (ParamArray Test)
<script type="text/javascript">
function invokeParamArrayMethod(func) {
var result = func();
document.getElementById('paramArrayResults').innerHTML = result;
}
</script>
<ul>
<li style="list-style:none;display:inline-block;">
<button onclick="invokeParamArrayMethod(function(){return bound.methodWithParams('test', 'hello-world')})">With 1 Params</button>
</li>
<li style="list-style:none;display:inline-block;">
<button onclick="invokeParamArrayMethod(function(){return bound.methodWithParams('test', 'hello-world', 'chris was here')})">With 2 Params</button>
</li>
<li style="list-style:none;display:inline-block;">
<button onclick="invokeParamArrayMethod(function(){return bound.methodWithParams('test')})">With no Params</button>
</li>
<li style="list-style:none;display:inline-block;">
<button onclick="invokeParamArrayMethod(function(){return bound.methodWithoutParams('test', 'hello')})">Normal Method</button>
</li>
<li style="list-style:none;display:inline-block;">
<button onclick="invokeParamArrayMethod(function(){return bound.methodWithoutAnything()})">Normal Method No Params</button>
</li>
</ul>
<span>ParamArray Results</span>
<br />
<span id="paramArrayResults"></span>
</p>
</body>
</html>
-8
View File
@@ -351,14 +351,6 @@
objects.
</p>
<h3>Hooks by which you can modify and/or override certain features of the web browsing</h3>
<p>
A fix to allow params to be used by the .NET methods called by the javascript<br />
<button onclick="boundEvent.methodWithParams('test', 'hello-world')">With 1 Params</button>
<button onclick="boundEvent.methodWithParams('test', 'hello-world', 'chris was here')">With 2 Params</button>
<button onclick="boundEvent.methodWithParams('test')">With no Params</button>
<button onclick="boundEvent.methodWithoutParams('test', 'hello')">Normal Method</button>
<button onclick="boundEvent.methodWithoutAnything()">Normal Method No Params</button>
</p>
</div>
<div class="bs-docs-section">
<div class="page-header">
@@ -36,26 +36,5 @@ namespace CefSharp.Example
EventArrived(eventName, eventData);
}
}
/// <summary>
/// Demonstrates the use of params as an argument in a bound object
/// </summary>
/// <param name="name">Dummy Argument</param>
/// <param name="args">Params Argument</param>
public void MethodWithParams(string name, params object[] args)
{
var test = args.ElementAtOrDefault(0);
var test2 = args.ElementAtOrDefault(1);
}
public void MethodWithoutParams(string name, string arg2)
{
var test = arg2;
}
public void methodWithoutAnything()
{
var test = "";
}
}
}
-1
View File
@@ -34,7 +34,6 @@ namespace CefSharp.Internals
[DataMember]
public string JavascriptName { get; set; }
[DataMember]
public List<MethodParameter> MethodParameters { get; set; }
/// <summary>
@@ -104,28 +104,37 @@ namespace CefSharp.Internals
try
{
//Fix for C# paramArray and refactor on arguments mismatch 03/22/2016
//Added param array support. Current implementation support param Object only. 04/04/2016
var missingParams = method.ParameterCount - parameters.Length;
if (missingParams > 0 || method.MethodParameters.Any(t => t.IsParamArray))
//CONDITION #1 : Check for parameter count missmatch between the parameters on the javascript function and the
// number of parameters on the bound object method.
//CONDITION #2 : Check if the bound object method contains a ParamArray as the last parameter on the method signature.
if (missingParams > 0 || method.MethodParameters.LastOrDefault(t => t.IsParamArray) != null)
{
var paramList = new List<object>();
var paramList = new List<object>(method.MethodParameters.Count);
for (int i = 0; i < method.MethodParameters.Count; i++)
//Loop through all of the method parameters on the bound object.
for (var i = 0; i < method.MethodParameters.Count; i++)
{
//Attempt to get the javascript function param at the current bound object parameter index.
//If the javascript function param is missing IE: NULL, then add Type.Missing.
//This will allow for default bound object parameters. IE: (string someParameter = "someValue")
object jsParam = parameters.ElementAtOrDefault(i);
if (jsParam == null && !method.MethodParameters[i].IsParamArray)
{
paramList.Add(Type.Missing);
}
//If the method parameter is a paramArray IE: (params Object[] someParameter)
//grab the parameters from the javascript function starting at the current bound object parameter index
//and add create an array that will be passed in as the last bound object method parameter.
else if (method.MethodParameters[i].IsParamArray)
{
List<object> convertedParams = new List<object>();
for (int s = i; s < parameters.Count(); s++)
for (var s = i; s < parameters.Length; s++)
{
convertedParams.Add(parameters[s]);
}
paramList.Add(convertedParams.ToArray());
break;
}
else
{
@@ -307,9 +316,7 @@ namespace CefSharp.Internals
jsMethod.ParameterCount = methodInfo.GetParameters().Length;
jsMethod.MethodParameters = methodInfo.GetParameters()
.Select(t => new MethodParameter()
{ Name = t.Name,
ParameterType = t.ParameterType,
Position = t.Position,
{
IsParamArray = t.GetCustomAttributes(typeof(ParamArrayAttribute), false).Length > 0
}).ToList();
+4 -12
View File
@@ -1,20 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
// Copyright © 2010-2016 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
namespace CefSharp.Internals
{
[DataContract]
public class MethodParameter
{
public String Name { get; set; }
public Type ParameterType { get; set; }
public Boolean IsParamArray { get; set; }
public Int32 Position { get; set; }
}
}
+3
View File
@@ -7,4 +7,7 @@
<add key="cefsharp-myget" value="https://www.myget.org/F/cefsharp/" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
<disabledPackageSources>
<add key="nuget.org" value="true" />
</disabledPackageSources>
</configuration>