gpt4 book ai didi

c# - 如何实现接受匿名类型的 C# 扩展方法

转载 作者:太空宇宙 更新时间:2023-11-03 21:58:22 25 4
gpt4 key购买 nike

在 ASP.NET MVC 中有大量的 Helpers 接受匿名类型作为参数并生成输入。

例如:

@Html.Action("Action", "Controler", new {routeVal1 = 1, routeVal2 = 2})

我的问题是如何实现我自己的接受匿名类型的扩展。我应该将它作为对象引用并使用反射还是有更优雅的解决方案?

最佳答案

您可以使用 dynamic而不是 object

Visual C# 2010 introduces a new type, dynamic. The type is a static type, but an object of type dynamic bypasses static type checking. In most cases, it functions like it has type object. At compile time, an element that is typed as dynamic is assumed to support any operation. Therefore, you do not have to be concerned about whether the object gets its value from a COM API, from a dynamic language such as IronPython, from the HTML Document Object Model (DOM), from reflection, or from somewhere else in the program. However, if the code is not valid, errors are caught at run time.

关于c# - 如何实现接受匿名类型的 C# 扩展方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11273091/

25 4 0
文章推荐: c# - 使用 IComparer 对 xml 中的节点进行排序/排序
文章推荐: css - 位置 : absolute in position: relative
文章推荐: html - 如何让