从 Javascript 接收 PageMethod 中的动态对象

发布于 2024-12-23 00:09:54 字数 1332 浏览 6 评论 0原文

我在 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“代理”来像动态对象一样访问字典)。

我只是想了解这一点。那么:

  1. 为什么 PageMethod 将其理解为字典?
  2. 有没有办法让它作为动态对象工作(“代理”方法除外)?

我希望这可以工作,因为我通常在服务器端定义那些 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:

  1. Why the PageMethod understands it as a Dictionary?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娜些时光,永不杰束 2024-12-30 00:09:54
  1. 页面方法将其视为字典的原因是 JavaScriptSerializer 内部的工作方式。由于所有 JSON 对象都可以表示为 Dictionary,JSS 的第一步是将 JSON 字符串转换为字典(如果没有注册目标类型的自定义转换器),并且您可以最终将该字典作为动态转换的最终结果。

  2. 不容易。

您可以尝试为 dynamic 编写自己的 JavaScriptConverter。这是我不久前写的同一大致内容的示例: https://gist.github.com/ 6cfcdfdf2a117fa5e81b

我的自定义序列化行为而不是反序列化,但您可以在 Deserialize 方法中实现自己的代码这样做。

  1. 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.

  2. 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/6cfcdfdf2a117fa5e81b

Mine's customizing the serialization behavior instead of deserialization, but you can implement your own code in the Deserialize method to do that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文