2013-10-04 24 views
0

我使用此ActionLink方法来生成方法。向ASP.NET MVC4中的对象routeValues自动添加匿名属性ActionLink

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

索所述第四参数是包含匿名属性,用于路由的Object。

它可以追加/自动添加新的匿名属性到现有的RouteValues是对象吗?

如果是,如何?

让我们假设我有一个方法:

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

如何为上述方法自动做到这一点?

谢谢

回答

0

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

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

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