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 <buchholz.bastian@googlemail.com>

Co-authored-by: Alex Maitland <amaitland@users.noreply.github.com>
Co-authored-by: campersau <buchholz.bastian@googlemail.com>
This commit is contained in:
pedoc
2021-04-05 08:29:42 +08:00
committed by Alex Maitland
parent 18790d1ca9
commit 1ff25441ea
3 changed files with 40 additions and 1 deletions
+1
View File
@@ -163,6 +163,7 @@
<Compile Include="Framework\RequestContextFacts.cs" />
<Compile Include="Framework\TestMemberInfo.cs" />
<Compile Include="JavascriptBinding\IntegrationTestFacts.cs" />
<Compile Include="JavascriptBinding\JavaScriptObjectRepositoryFacts.cs" />
<Compile Include="OffScreen\OffScreenBrowserBasicFacts.cs" />
<Compile Include="CefSharpXunitTestFramework.cs" />
<Compile Include="CefSharpFixture.cs" />
@@ -0,0 +1,38 @@
using CefSharp.Internals;
using Xunit;
internal class NoNamespaceClass
{
public SomeElseClass SomeElseClass { get; set; }
public int Year { get; set; }
public string GetExampleString()
{
return "ok";
}
}
internal class SomeElseClass
{
}
namespace CefSharp.Test.JavascriptBinding
{
public class JavaScriptObjectRepositoryFacts
{
[Fact]
public void CanRegisterJavascriptObjectBindWhenNamespaceIsNull()
{
var javascriptObjectRepository = new JavascriptObjectRepository();
var name = nameof(NoNamespaceClass);
javascriptObjectRepository.Register(name, new NoNamespaceClass(), false, new BindingOptions() { });
Assert.True(javascriptObjectRepository.IsBound(name));
var result = ((IJavascriptObjectRepositoryInternal)javascriptObjectRepository).TryCallMethod(1, "getExampleString", new object[0]);
Assert.True(result.Success);
Assert.Equal("ok", result.ReturnValue.ToString());
}
}
}
@@ -738,7 +738,7 @@ namespace CefSharp.Internals
baseType = Nullable.GetUnderlyingType(type);
}
if (baseType == null || baseType.IsArray || baseType.Namespace.StartsWith("System"))
if (baseType == null || baseType.IsArray || baseType.Namespace?.StartsWith("System") == true)
{
return false;
}