c# - 简单对象和数组是如何传递的?

标签 c# javascript .net silverlight marshalling

我对负责将简单 Javascript 对象和数组转换为“托管类型”(为了简洁起见,C#)对象的转换机制有点困惑。

第一节Microsoft article concerning this声明(或似乎暗示)转换是隐式且自动的:

  • JavaScript arrays are converted to object[].
  • JavaScript dictionaries are converted to Dictionary.

但是,后续部分似乎提到了辅助函数 createcreateObject 转换的必要性:

数组:

Array and List Types

The default is explicit by-value marshaling from JavaScript to managed code. Automatic JSON serialization to managed input parameters is not supported. Instead, you must explicitly use creatable types or the create helper methods.

 ...
 ...

By-Value Marshaling

If you want to pass a JavaScript array by value to managed code, the target .NET Framework property or input parameter must be typed as something other than ScriptObject. You must then use one of the create or createObject helper methods to explicitly force a conversion from the JavaScript array to a .NET Framework list or array type.

(“字典和自定义类型”有一对几乎相同的子句)

我是否遗漏了什么,或者文档是否矛盾?对于简单对象和数组来说,转换是否是隐式和自动的?如果是,后面的条款适用于哪些对象?如果没有,为什么在隐式转换的原始 Javascript 类型中提到它们?

最佳答案

1)答案已在文章中给出。您没有引用的一段话。

The JavaScript dictionary will be recursively marshaled by value onto the target .NET Framework type. Silverlight will use the JSON serializer to deserialize from a JavaScript dictionary onto a conforming .NET Framework custom type or dictionary.

2) 因为自定义类型可能位于已知类型内。 如果您是这样的异教徒,.net 确保您可以随意绕过转换。我说异端,既然如此,为什么还要用json呢? =)

让它滑动..

关于c# - 简单对象和数组是如何传递的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14426279/

相关文章:

.NET COM 互操作 -> 全局异常处理程序

c# - Blazor 父组件在子组件发生 onclick 事件后意外得到重新初始化

c# - Unity3d、C# : Totally lost with ridiculous errors

c# - MEF 2 : import many

C#时区计算问题

c# - 如何创建动态表达式,如 ( x, y ) => DateTime.Compare( y.EFFECTIVE_DATE, x.EFFECTIVE_DATE )

javascript - Node.js 客户端重新连接的唯一标识符

循环内的 JavaScript 闭包——简单实用的例子

javascript - 如何停止正在 Chrome 中调试的 JavaScript 程序?

.net - 需要异常号而不是消息字符串