I am using Blazor javascript interop (js call to .NET), as in
//js
dotNetObjRef.invokeMethodAsync("DotNetMethod", jsObject);
---------------------------------------------------------
//C#
[JSInvokable]
public void DotNetMethod(object jsObject)
{
Console.WriteLine($"jsObject type is {jsObject.GetType()}");
}
in the browser console, I get:
'jsObject type is SimpleJson.JsonObject'
Now I would like to cast jsObject to a concrete SimpleJson.JsonObject, as in
[JSInvokable]
public void DotNetMethod(object jsObject)
{
JsonObject jsObject = (JsonObject)jsObject; //error
}
but all my trials using C# munity implementations of SimpleJson (like ) fail plaining that the cast is not valid.
As a workaround I go through strings:
//js
dotNetObjRef.invokeMethodAsync("DotNetMethod", JSON.stringify(jsObject));
.
//C#
[JSInvokable]
public void DotNetMethod(string jsObjectJSON)
{
JsonObject jsObject = SimpleJson.DeserializeObject<JsonObject>(jsObjectJSON);
}
Does anyone know whether it is possible (and how) to use the received jsObject directly, i.e. avoiding the serialization/deserialization (and without reflection)?
I am using Blazor javascript interop (js call to .NET), as in
//js
dotNetObjRef.invokeMethodAsync("DotNetMethod", jsObject);
---------------------------------------------------------
//C#
[JSInvokable]
public void DotNetMethod(object jsObject)
{
Console.WriteLine($"jsObject type is {jsObject.GetType()}");
}
in the browser console, I get:
'jsObject type is SimpleJson.JsonObject'
Now I would like to cast jsObject to a concrete SimpleJson.JsonObject, as in
[JSInvokable]
public void DotNetMethod(object jsObject)
{
JsonObject jsObject = (JsonObject)jsObject; //error
}
but all my trials using C# munity implementations of SimpleJson (like https://github./facebook-csharp-sdk/simple-json) fail plaining that the cast is not valid.
As a workaround I go through strings:
//js
dotNetObjRef.invokeMethodAsync("DotNetMethod", JSON.stringify(jsObject));
.
//C#
[JSInvokable]
public void DotNetMethod(string jsObjectJSON)
{
JsonObject jsObject = SimpleJson.DeserializeObject<JsonObject>(jsObjectJSON);
}
Does anyone know whether it is possible (and how) to use the received jsObject directly, i.e. avoiding the serialization/deserialization (and without reflection)?
Share Improve this question asked May 31, 2019 at 3:33 Franco TiveronFranco Tiveron 2,9361 gold badge27 silver badges44 bronze badges 2-
Typically JSInterop does require you to go through string serialization. In fact, that you want to interact with the parameters as a
JsonObject
seems to imply that's exactly what you're trying to do anyway. So I'm a bit confused why you think your workaround isn't perfectly proper? – Kirk Woll Commented Jun 1, 2019 at 2:03 - Blazor provides JS interop, including implicit transformations CLI <=> JSON. If I know the class signature of the type I can pass instances between the two worlds and blazor automatically transforms them. When I saw that the type of the object ing from js to C# is JsonObject I hoped I could rely on Blazor built-in capabilities and simply cast to JsonObject instead of manually serializing/deserializing it, but apparently Blazor looks for a strongly typed destination. In other words, If I want a weakly type representation like JsonObject, I have to be explicit. Not a big issue, though. – Franco Tiveron Commented Jun 14, 2019 at 0:53
1 Answer
Reset to default 7Instead of directly receiving a JSON, why not receive a previously defined object?
let x = {
"data": "test",
"moreData": "another test"
}
dotnetHelper.invokeMethodAsync('selectionChanged', x);
And then
//C#
public class InteropHelper
{
public event Action OnSelectionChanged;
[JSInvokable]
public void selectionChanged(TestItem data)
{
OnSelectionChanged?.Invoke();
}
class TestItem
{
public string data { get; set; }
public string moreData { get; set; }
}
}
Blazor would automatically know which fields from the JSON go with which fields from TestItem.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744270933a4566097.html
评论列表(0条)