Skip to content

Commit 893d032

Browse files
pedocamaitlandcampersau
authored
Javascript Binding - Allow binding to classes in the global namespace (#3467)
* fix IsComplexType NullReferenceException Sometimes, the registered Javascript object does not have a namespace, at this time this function will throw a ```NullReferenceException```. * add unit test for IsComplexType * Simplify and remove useless code * Update CefSharp/Internals/JavascriptObjectRepository.cs Co-authored-by: campersau <[email protected]> Co-authored-by: Alex Maitland <[email protected]> Co-authored-by: campersau <[email protected]>
1 parent 462bc74 commit 893d032

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

CefSharp.Test/CefSharp.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
<Compile Include="Framework\RequestContextFacts.cs" />
164164
<Compile Include="Framework\TestMemberInfo.cs" />
165165
<Compile Include="JavascriptBinding\IntegrationTestFacts.cs" />
166+
<Compile Include="JavascriptBinding\JavaScriptObjectRepositoryFacts.cs" />
166167
<Compile Include="OffScreen\OffScreenBrowserBasicFacts.cs" />
167168
<Compile Include="CefSharpXunitTestFramework.cs" />
168169
<Compile Include="CefSharpFixture.cs" />
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using CefSharp.Internals;
2+
using Xunit;
3+
4+
internal class NoNamespaceClass
5+
{
6+
public SomeElseClass SomeElseClass { get; set; }
7+
public int Year { get; set; }
8+
9+
public string GetExampleString()
10+
{
11+
return "ok";
12+
}
13+
}
14+
15+
internal class SomeElseClass
16+
{
17+
18+
}
19+
20+
21+
namespace CefSharp.Test.JavascriptBinding
22+
{
23+
public class JavaScriptObjectRepositoryFacts
24+
{
25+
[Fact]
26+
public void CanRegisterJavascriptObjectBindWhenNamespaceIsNull()
27+
{
28+
var javascriptObjectRepository = new JavascriptObjectRepository();
29+
var name = nameof(NoNamespaceClass);
30+
javascriptObjectRepository.Register(name, new NoNamespaceClass(), false, new BindingOptions() { });
31+
Assert.True(javascriptObjectRepository.IsBound(name));
32+
33+
var result = ((IJavascriptObjectRepositoryInternal)javascriptObjectRepository).TryCallMethod(1, "getExampleString", new object[0]);
34+
Assert.True(result.Success);
35+
Assert.Equal("ok", result.ReturnValue.ToString());
36+
}
37+
}
38+
}

CefSharp/Internals/JavascriptObjectRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ private static bool IsComplexType(Type type)
738738
baseType = Nullable.GetUnderlyingType(type);
739739
}
740740

741-
if (baseType == null || baseType.IsArray || baseType.Namespace.StartsWith("System"))
741+
if (baseType == null || baseType.IsArray || baseType.Namespace?.StartsWith("System") == true)
742742
{
743743
return false;
744744
}

0 commit comments

Comments
 (0)