Silverlight

If the target managed property or input parameter is strongly typed (that is, not typed as an object), Silverlight attempts to convert the JavaScript object to the corresponding .NET Framework object. If the conversion fails (because of mismatched types or overflows), an exception is returned to the JavaScript caller.

Type conversions are described in the following sections:

Note:

 Silverlight for Windows Phone does not support the HTML Bridge feature.

Primitive Types

 
 

Primitive JavaScript types are converted to their best-match counterparts in the .NET Framework. In the list below, an asterisk (*) indicates that this rule also applies to return values from the GetProperty, Invoke, and InvokeSelf methods.

  • JavaScript strings* are converted to .NET Framework strings. They can be automatically converted to Type objects if the target input parameter on a scriptable method is typed as Type.
  • JavaScript null values* are passed as null values.
  • JavaScript Boolean values* are converted to .NET Framework Boolean values.
  • JavaScript Date objects* are converted to .NET Framework DateTime objects using UTC times when the target input parameter or property is explicitly typed as DateTime. When JavaScript Date objects are obtained by using the loosely typed ScriptObject methods, the JavaScript Date object is returned as a ScriptObject. In this scenario, you can use the static ConvertToDateTime helper method on ScriptObject to explicitly convert to a DateTime.
  • JavaScript strings can be converted to .NET Framework DateTime properties or input parameters automatically, by using the Parse class.
  • JavaScript numbers* can be automatically converted to a conforming target .NET Framework types (numeric type, one of the byte types, enumeration, or string). If the JavaScript number cannot be converted successfully, an exception is returned to the JavaScript caller. If the target .NET Framework type is just object, the following "best fit" conversion rule is used:
  • JavaScript numbers are returned as Double values. Firefox always returns integer values that are greater than 2^31 as doubles, whereas Safari deals only with doubles. The Double return type guarantees the same behavior across browsers. (Note that both JavaScript and the .NET Framework support the IEEE 754 floating-point standard, so a non-integer in JavaScript maps cleanly to the Double data type.) Managed code developers have to explicitly convert the returned double value into an integer. However, as noted in Returning or Passing Managed Types to JavaScript, you must also be aware of the potential loss of precision inherent with the double data type.
  • JavaScript strings can be automatically converted to .NET Framework properties or input parameters that are typed as Guid. The following pattern must be used: "382c74c3-721d-4f34-80e5-57657b6cbc27".
  • .NET Framework properties or input parameters typed as object are converted using the following heuristics when marshaling by value to a target .NET Framework property or input parameter:
  • JavaScript arrays are converted to object[].
  • JavaScript dictionaries are converted to Dictionary<string,object>.
  • .NET Framework properties or input parameters that are structures are marshaled by value from a JavaScript object. Only public properties and fields on the target structure are candidates for matching JavaScript properties. If the structure does not have a corresponding public property or field for a JavaScript property, the JavaScript property value is silently ignored. You still have to use createManagedObject or createObject to marshal the JavaScript object to a managed structure. Structures are marshaled between managed code and JavaScript as follows:
    • Passing a structure from managed code to JavaScript results in by-value behavior. The structure value is explicitly unboxed and copied over to JavaScript.
    • Passing a JavaScript object to managed code, where the target type is a structure, results in by-value behavior and requires an explicit call to createManagedObject.
    • Passing a JavaScript object to managed code, where the target type is Object, results in a by-reference handle. This enables both the JavaScript caller and the managed call point to manipulate the same structure.

Well-Known Types

 
 

Silverlight handles the following well-known JavaScript objects as special cases and returns them as the appropriate HTML Bridge object reference instead:

  • References to HTML elements are returned as HtmlElement.
  • Collections of HTML elements are returned as a ScriptObjectCollection.
  • References to the containing document are returned as HtmlDocument.
  • References to HTML objects other than elements or the document are returned as HtmlObject.
  • Arbitrary JavaScript objects can be returned as ScriptObject references. For information about how to obtain ScriptObject references to arbitrary JavaScript objects, see the sections on arrays and dictionaries later in this topic.

Complex Types

 
 

The following table provides conversion information for complex types.

JavaScript variable

Target .NET Framework type

Result

JavaScript array/dictionary

.NET Framework array/list type on a [ScriptableMember] property or parameter on a [ScriptableMember] method.

You must wrap the JavaScript array/dictionary with a call to the create methods and related helper methods to convert it to a managed type.

JavaScript array/dictionary

Object on a [ScriptableMember] property or parameter on a [ScriptableMember] method.

You must wrap the JavaScript array/dictionary with a call to the create methods and related helper methods to convert it to a managed type.

JavaScript array/dictionary

ScriptObject . This could be a return value or used as the type for a [ScriptableMember] property or parameter on a [ScriptableMember] method.

A ScriptObject reference for which ManagedObject is null.

Array/dictionary interop wrapper

.NET Framework array/list type on a [ScriptableMember] property or parameter on a [ScriptableMember] method.

A reference to the concrete underlying .NET Framework type, assuming the type exists in the current application domain.

Array/dictionary interop wrapper

Object on a [ScriptableMember] property or parameter on a [ScriptableMember] method.

A reference to the concrete underlying .NET Framework type, assuming the type exists in the current application domain. You must explicitly cast to get to the underlying type.

Array/dictionary interop wrapper

ScriptObject . This could be a return value, or used as the type for a [ScriptableMember] property or parameter on a [ScriptableMember] method.

A ScriptObject reference on which ManagedObject is non-null. You can cast the ManagedObject property to get to the underlying type, assuming the type exists in the current application domain.

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-Reference Marshaling

You can make a JavaScript array available by reference to managed code in two ways:

  • The target .NET Framework property or input parameter can be typed as ScriptObject. Managed code can then manipulate the JavaScript array by using the generic get, set, and invoke methods that are exposed from ScriptObject. You can also use the ManagedObject property to get the underlying managed reference.
  • If the JavaScript array is an array-interop wrapper (see Returning or Passing Managed Types to JavaScript), Silverlight will simply assign the underlying managed array/list reference to a .NET Framework property or input parameter, assuming that the type exists in the current application domain. If you want to round-trip the interop wrapper and have it automatically surfaced with the correct type, you must type the target property or input parameter as the correct .NET Framework type instead of a ScriptObject. If there is a type mismatch between the array-interop wrapper and the target type, an exception is returned to the JavaScript caller.

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. This has the effect of disconnecting the JavaScript representation of the data from the .NET Framework representation.

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

Dictionaries and Custom 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 the create or createObject helper methods.

By-Reference Marshaling

You can make a JavaScript dictionary available by reference to managed code by using one of the following two methods:

  • The target .NET Framework property or input parameter can be typed as ScriptObject. Managed code can then manipulate the JavaScript dictionary by using the generic get, set, and invoke methods that ScriptObject exposes. You can also decide to use the ManagedObject property to get to the underlying managed reference.
  • If the JavaScript dictionary is actually a dictionary-interop wrapper (see Returning or Passing Managed Types to JavaScript), Silverlight will simply assign the underlying managed dictionary reference to a .NET Framework property or input parameter, assuming that the type exists in the current application domain. If you want to round-trip the interop wrapper and have it automatically surfaced with the correct type, you must type the target property or input parameter as the correct .NET Framework type instead of a ScriptObject. If there is a type mismatch between the dictionary-interop wrapper and the target type, an exception is returned to the JavaScript caller.

By-Value Marshaling

If you want to pass a JavaScript dictionary 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 dictionary to a .NET Framework custom type or dictionary type. This has the effect of disconnecting the JavaScript representation of the data from the .NET Framework representation.

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.

 
 

从 <http://msdn.microsoft.com/zh-cn/library/cc645079(v=vs.95).aspx> 插入

Passing JavaScript Objects to Managed Code的更多相关文章

  1. Optimize Managed Code For Multi-Core Machines

    Parallel Performance Optimize Managed Code For Multi-Core Machines Daan Leijen and Judd Hall This ar ...

  2. Post Complex JavaScript Objects to ASP.NET MVC Controllers

    http://www.nickriggs.com/posts/post-complex-javascript-objects-to-asp-net-mvc-controllers/     Post ...

  3. JavaScript Objects in Detail

    JavaScript’s core—most often used and most fundamental—data type is the Object data type. JavaScript ...

  4. javascript - Get page source code - Stack Overflow

    javascript - Get page source code - Stack Overflow Get page source code

  5. dinoql 使用graphql 语法查询javascript objects

    dinoql 是一个不错的基于graphql 语法查询javascript objects 的工具包,包含以下特性 graphql 语法(很灵活) 安全的访问(当keys 不存在的时候,不会抛出运行时 ...

  6. 不要忽视Managed code stripping的副作用

    0x00 前言 Unity 2018.3之后,新的“Managed Stripping Level”选项将替换 player settings 中原有的“Stripping Level”选项. 这个新 ...

  7. How to: Synchronize Files by Using Managed Code

    The examples in this topic focus on the following Sync Framework types: FileSyncProvider FileSyncOpt ...

  8. [Immutable.js] Using fromJS() to Convert Plain JavaScript Objects into Immutable Data

    Immutable.js offers the fromJS() method to build immutable structures from objects and array. Object ...

  9. [Javascript] Understanding the .constructor property on JavaScript Objects

    Constructor functions hold an interesting purpose in JavaScript. Unlike in classical languages, they ...

随机推荐

  1. windows下将多个文件里面的内容合并成一个一个文件

    如题:例如有多个章节的小说,现在要把他们合并成一个txt文件. 利用windows自带cmd工具: 一.拷贝合并1.将你的txt文档按照顺序分别命名为01.txt 02.txt 03.txt……2.将 ...

  2. 20、android解决方案(转载)

    目录: 1.广告 2.推送 3.云 4.统计 5.后端存储 6.地图 7.测试 8.托管 9.支付 10.音视频 11.社会化分享 12.存储 13.自动更新 14.轻开发 15.安全 16.图像 1 ...

  3. 使用CSS禁止textarea调整大小功能的方法

    这篇文章主要介绍了使用CSS禁止textarea调整大小功能的方法,禁止可以调整textarea大小功能的方法很简单,使用CSS的resize属性即可,需要的朋友可以参考下 如果你使用谷歌浏览器或火狐 ...

  4. 项目前端技术-learn

    赶鸭子上架 之 前端学习 目前项目中的前端用到的技术主要是:1. bootstrap框架; 2. 基于javscript的jQuery, jQuery ui; 3. dust前端模板引型.

  5. java 解决JFrame不能设置背景色的问题 分类: Java Game 2014-08-15 09:48 119人阅读 评论(0) 收藏

    这段时间比较多,于是写一写JAVA的一些IT技术文章.如有JAVA高手请加QQ:314783246,互相讨论. 在Java的GUI设计中,Frame和JFrame两者之间有很大差别,上次刚学时编一个窗 ...

  6. VC++之GetLastError()使用说明

    VC中GetLastError()获取错误信息的使用 在VC中编写应用程序时,经常需要涉及到错误处理问题.许多函数调用只用TRUE和FALSE来表明函数的运行结果.一旦出现错误,MSDN中往往会指出请 ...

  7. CSL概述(翻译总结自TI官方文档)

    一.简单介绍 CSL是函数,宏和符号常数的集合,用来控制和配置片上外设.(Chip Support Library) 每一个外设都有自己对应的CSL模块.每个模块有自己的支持符,来表示对于给定的设备, ...

  8. Apache CXF实现Web Service(3)——Tomcat容器和不借助Spring的普通Servlet实现JAX-RS(RESTful) web service

    起步 参照这一系列的另外一篇文章: Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 首先 ...

  9. Access数据库和SQL Server数据库在实际应用中的区别

    1.在Access数据库中简历查询语句的步骤 --> 打开你的MDB --> 在数据库窗口中,点击“查询”,或在“视图”菜单中选择“数据库对象”-> “查询” --> 点击数据 ...

  10. 白话CSS3的新特性

    声明:这篇文章不是手册,所以不会说的很详细,只是告诉初学者CSS3显著的改进有啥,高手老手绕行. 一.在边框上的改进 1.可以给方框加圆角了,值越大越圆,解决了过去大方框的不美观 2.可以给控件加阴影 ...