将自动匿名属性添加到 routeValues ASP.NET MVC4 ActionLink 中的对象

本文关键字:MVC4 NET ASP ActionLink 对象 routeValues 添加 属性 | 更新日期: 2023-09-27 18:33:49

我使用这个ActionLink方法来生成方法。

LinkExtensions.ActionLink Method (HtmlHelper, String, String, Object)

因此,第 4 个参数是一个包含匿名属性的对象,用于路由。

可以将新的匿名属性自动附加/添加到现有的路由值中,即对象?

如果是,如何?

假设我有一个方法:

public void Test( ref object currentRouteValues, string newValue)
{
    if(!string.IsNullOrEmpty(newValue)){
       // add a custom property here to currentRouteValues
       // something like: (is wrong but I don't know how to proceed)
       currentRouteValues = new { currentRouteValues, myCustoProperty = newValue };
    }
}

如何自动为上述方法执行此操作?

谢谢

将自动匿名属性添加到 routeValues ASP.NET MVC4 ActionLink 中的对象

我想这会回答你的问题。

合并匿名类型

如果您只想提取数据,它将是这样的。

        Object o = new { var1 = "the first var", var2 = "the second var" };
        System.Type type = o.GetType();
        foreach (var i in type.GetProperties())
        {
            Console.Write(i.GetValue(o));
        }

但是对于合并,请查看上面的链接。