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

时间:2013-10-04 09:45:49

标签: c# asp.net asp.net-mvc asp.net-mvc-4

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

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

Sot第4个参数是一个包含匿名属性的Object,用于路由。

可以将新的匿名属性自动附加/添加到现有的routeValues,即Object吗?

如果是,怎么样?

我们假设我有一个方法:

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 };
    }
}

如何自动执行上述方法?

由于

1 个答案:

答案 0 :(得分:1)

我认为这会回答你的问题。

Merging anonymous types

如果您只是想提取数据,那就是这样的。

        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));
        }

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