Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support serialization of CLR Classes #2025

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CefSharp.BrowserSubprocess.Core/TypeUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ namespace CefSharp

return cefArray;
}
if (type->IsValueType && !type->IsPrimitive && !type->IsEnum)
if (!type->IsPrimitive && !type->IsEnum)
{
cli::array<System::Reflection::FieldInfo^>^ fields = type->GetFields();
CefRefPtr<CefV8Value> cefArray = CefV8Value::CreateArray(fields->Length);
Expand Down
2 changes: 1 addition & 1 deletion CefSharp.Core/Internals/Serialization/V8Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ namespace CefSharp
}
list->SetList(index, subList);
}
else if (type->IsValueType && !type->IsPrimitive && !type->IsEnum)
else if (!type->IsPrimitive && !type->IsEnum)
{
auto fields = type->GetFields();
auto subDict = CefDictionaryValue::Create();
Expand Down
19 changes: 12 additions & 7 deletions CefSharp.Example/AsyncBoundObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

namespace CefSharp.Example
{
public struct JsObject
{
public string Value;
}

public class AsyncBoundObject
{
Expand Down Expand Up @@ -39,12 +35,21 @@ public void DoSomething()
Thread.Sleep(1000);
}

public JsObject[] ObjectArray(string name)
public JsSerializableStruct[] StructsArray(JsSerializableStruct obj)
{
return new[]
{
new JsObject() { Value = "Item1" },
new JsObject() { Value = "Item2" }
new JsSerializableStruct() { Value = obj.Value + "1" },
new JsSerializableStruct() { Value = obj.Value + "2" }
};
}

public JsSerializableClass[] ClassesArray(string name)
{
return new[]
{
new JsSerializableClass() { Value = name + "1" },
new JsSerializableClass() { Value = name + "2" }
};
}
}
Expand Down
2 changes: 2 additions & 0 deletions CefSharp.Example/CefSharp.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
<Compile Include="Filters\PassThruResponseFilter.cs" />
<Compile Include="Handlers\BrowserProcessHandler.cs" />
<Compile Include="InMemorySchemeAndResourceHandlerFactory.cs" />
<Compile Include="JsSerializableStruct.cs" />
<Compile Include="JsSerializableClass.cs" />
<Compile Include="RequestEventHandler\EventArgs\BaseRequestEventArgs.cs" />
<Compile Include="RequestEventHandler\EventArgs\GetAuthCredentialsEventArgs.cs" />
<Compile Include="RequestEventHandler\EventArgs\GetResourceResponseFilterEventArgs.cs" />
Expand Down
7 changes: 7 additions & 0 deletions CefSharp.Example/JsSerializableClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace CefSharp.Example
{
public class JsSerializableClass
{
public string Value;
}
}
7 changes: 7 additions & 0 deletions CefSharp.Example/JsSerializableStruct.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace CefSharp.Example
{
public struct JsSerializableStruct
{
public string Value;
}
}
18 changes: 14 additions & 4 deletions CefSharp.Example/Resources/BindingTest.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,32 @@
});
}

function asyncObjectArray()
function asyncStructsArray()
{
var call = "Async call (ObjectArray): " + Date();
boundAsync.objectArray('CefSharp').then(function (res)
var call = "Async call (StructsArray): " + Date();
boundAsync.structsArray({ Value: 'CefSharp' }).then(function (res)
{
var end = "Result: [ " + res.map(function (item) { return item.Value }) + " ] (" + Date() + ")";
writeAsyncResult(call, end);
});
}

function asyncClassesArray()
{
var call = "Async call (ClassesArray): " + Date();
boundAsync.classesArray('CefSharp').then(function (res) {
var end = "Result: [ " + res.map(function (item) { return item.Value }) + " ] (" + Date() + ")";
writeAsyncResult(call, end);
});
}

asyncError();
asyncDivOk();
asyncDivFail();
asyncDoSomething();
asyncHello();
asyncObjectArray();
asyncStructsArray();
asyncClassesArray();
</script>
</p>
<p>
Expand Down
2 changes: 1 addition & 1 deletion CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public BrowserTabView()

browser.RequestHandler = new RequestHandler();
browser.RegisterJsObject("bound", new BoundObject(), BindingOptions.DefaultBinder);
browser.RegisterAsyncJsObject("boundAsync", new AsyncBoundObject());
browser.RegisterAsyncJsObject("boundAsync", new AsyncBoundObject(), BindingOptions.DefaultBinder);
// Enable touch scrolling - once properly tested this will likely become the default
//browser.IsManipulationEnabled = true;

Expand Down
38 changes: 1 addition & 37 deletions CefSharp/ModelBinding/ModelBindingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static T CreateInstance<T>(this Type type, bool nonPublic = false)
/// <param name="nonPublic"><see langword="true"/> if a non-public constructor can be used, otherwise <see langword="false"/>.</param>
public static object CreateInstance(this Type type, bool nonPublic = false)
{
return CreateInstanceInternal(type, nonPublic);
return Activator.CreateInstance(type, nonPublic);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this change? How does it relate to structs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Structs have implicit public parameterless constructors and the CreateInstanceInternal wasn't able to find that constructor. So I replaced with the Activator.CreateInstance which does the job right.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much clearer, thank you.

}

/// <summary>
Expand Down Expand Up @@ -217,41 +217,5 @@ public static TypeCode GetTypeCode(this Type type)
else
return TypeCode.Object;
}

private static object CreateInstanceInternal(Type type, bool nonPublic = false)
{
var constructor = type.GetDefaultConstructor(nonPublic);

if (constructor == null)
{
throw new MissingMethodException("No parameterless constructor defined for this object.");
}

return constructor.Invoke(new object[0]);
}

private static ConstructorInfo GetDefaultConstructor(this Type type, bool nonPublic = false)
{
var typeInfo = type.GetTypeInfo();

var constructors = typeInfo.DeclaredConstructors;

foreach (var constructor in constructors)
{
var parameters = constructor.GetParameters();

if (parameters.Length > 0)
{
continue;
}

if (!constructor.IsPrivate || nonPublic)
{
return constructor;
}
}

return null;
}
}
}