从 Javascript 接收 PageMethod 中的动态对象
我在 Javascript 中创建了一个 new object()
,将一些值放入其中并将其传递给 PageMethod
...
var filters = new object();
filters.Name = $("#tbxName").val();
filters.Age = parseInt($("#tbxAge").val());
PageMethods.getPeople(filters, getPeopleCallback);
... 在其签名中,它接收一个 <代码>动态参数。
[WebMethod]
public static object getPeople(dynamic filters)
{
...
整个代码编译和运行没有任何问题,除了服务器端 PageMethod 之外,它将 dynamic
参数理解为 Dictionary
。
当我调试代码以查看 filters
对象的定义时,它显示以下类型:filters = {System.Collections.Generic.Dictionary
所以,处理它的唯一方法是将它用作Dictionary
.....
var name = filters["Name"];
var age = filters["Age"] as int?;
但我的意图是将它用作动态对象
var name = filters.Name;
var age = filters.Age;
我知道这根本不是什么大问题,绝对没有问题我将其作为字典访问(而且我也知道我可以创建一个DynamicObject
“代理”来像动态对象一样访问字典)。
我只是想了解这一点。那么:
- 为什么 PageMethod 将其理解为字典?
- 有没有办法让它作为动态对象工作(“代理”方法除外)?
我希望这可以工作,因为我通常在服务器端定义那些 Filter
类,并按照相同的结构在 Javascript 中创建它,当我将该类型定义为 a 时,PageMethod 会理解它并正确转换它参数(例如:getPeople(Filtersfilters)
)。所以,我想知道它是否也可以对动态对象执行此操作(节省我的时间和不必要的类)。
感谢您的时间和帮助。
I created a new object()
in Javascript, put some values into it and passed it to a PageMethod
...
var filters = new object();
filters.Name = $("#tbxName").val();
filters.Age = parseInt($("#tbxAge").val());
PageMethods.getPeople(filters, getPeopleCallback);
... which, in it's signature, receives an dynamic
argument.
[WebMethod]
public static object getPeople(dynamic filters)
{
...
The whole code compiles and run with no problems, except for the server side PageMethod, which understands the dynamic
parameter as a Dictionary
.
When I debug the code to see the definition of the filters
object, it shows me the following type: filters = {System.Collections.Generic.Dictionary<string,object>}
So, the only way deal with it is using it as a Dictionary
...
var name = filters["Name"];
var age = filters["Age"] as int?;
.. But my intention was to use it as a Dynamic object
var name = filters.Name;
var age = filters.Age;
I know that this is not a big deal at all, and there is absolutely no problem for me to access it as a Dictionary (and I also know I can make a DynamicObject
"Proxy" to access the Dictionary like a dynamic object).
I just want to understand this. So:
- Why the PageMethod understands it as a Dictionary?
- Is there a way to make this work as a Dynamic object (except for the "Proxy" approach)?
I was hoping that this could work because I usually define those Filter
Classes on Server Side and create it in Javascript following the same structure, and the PageMethod understands it and convert it correctly when I define that Type as a argument (eg: getPeople(Filters filters)
). So, I was wondering if it could do that also with dynamic objects (saving me time and unnecessary classes).
I appreciate your time and help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
页面方法将其视为字典的原因是 JavaScriptSerializer 内部的工作方式。由于所有 JSON 对象都可以表示为
Dictionary
,JSS 的第一步是将 JSON 字符串转换为字典(如果没有注册目标类型的自定义转换器),并且您可以最终将该字典作为动态转换的最终结果。不容易。
您可以尝试为
dynamic
编写自己的 JavaScriptConverter。这是我不久前写的同一大致内容的示例: https://gist.github.com/ 6cfcdfdf2a117fa5e81b我的自定义序列化行为而不是反序列化,但您可以在 Deserialize 方法中实现自己的代码这样做。
The reason that the page method treats that as a Dictionary is because of how JavaScriptSerializer works internally. Since all JSON objects can be represented as a
Dictionary<string, object>
, JSS' first pass is to convert the JSON string into a Dictionary (if no custom converters for the target type are registered) and you end up with that Dictionary as the end result of the conversion for dynamics.Not easily.
You could experiment with writing your own JavaScriptConverter for
dynamic
. This is an example of something in the same ballpark that I wrote a while back: https://gist.github.com/6cfcdfdf2a117fa5e81bMine's customizing the serialization behavior instead of deserialization, but you can implement your own code in the Deserialize method to do that.