C# 解析C“方法参数”;参数对象[]”;

C# 解析C“方法参数”;参数对象[]”;,c#,methods,parameters,C#,Methods,Parameters,很简单,我想将objects[]参数解析为它的值。如果我传入这个值,它将返回“{id=3}”。我想做字符串id=3。。。这怎么可能 NETMVC这样做的原因是:Url.Action(ActionName,ControllerName,new{id=3}) 我想得到一个Annonymous类型的值 GetUrlStringStringObjectArray = (string actionName, string controllerName, **object[] parameters**) =

很简单,我想将objects[]参数解析为它的值。如果我传入这个值,它将返回“{id=3}”。我想做字符串id=3。。。这怎么可能

NETMVC这样做的原因是:Url.Action(ActionName,ControllerName,new{id=3})

我想得到一个Annonymous类型的值

GetUrlStringStringObjectArray = (string actionName, string controllerName, **object[] parameters**) =>
{
    Assert.AreEqual<string>(EventsController.Actions.Register.ToString(), actionName, "Url.Action called with an incorrect action.");
    Assert.AreEqual<string>(EventsController.ControllerName, controllerName, "Url.Action called with an incorrect controller.");

    string id = parameters[0].ToString();
    // returns "{ id = 3}"

    if (!String.IsNullOrWhiteSpace(id)) 
        return String.Format("/{0}/{1}/{2}", controllerName, actionName, id);
    else
        return String.Format("/{0}/{1}", controllerName, actionName);
}
GetUrlStringObjectArray=(字符串actionName,字符串controllerName,**对象[]参数**)=>
{
Assert.AreEqual(EventsController.Actions.Register.ToString(),actionName,“Url.Action被错误的操作调用”);
AreEqual(EventsController.ControllerName,ControllerName,“Url.Action使用不正确的控制器调用”);
字符串id=参数[0]。ToString();
//返回“{id=3}”
如果(!String.IsNullOrWhiteSpace(id))
返回String.Format(“/{0}/{1}/{2}”,controllerName,actionName,id);
其他的
返回String.Format(“/{0}/{1}”,controllerName,actionName);
}

将方法更改为采用RouteValueDictionary,而不是对象数组

var url = GetUrlStringStringObjectArray = (string actionName, string controllerName, RouteValueDictionary parameters) => {
    Assert.AreEqual<string>(EventsController.Actions.Register.ToString(), actionName, "Url.Action called with an incorrect action.");
    Assert.AreEqual<string>(EventsController.ControllerName, controllerName, "Url.Action called with an incorrect controller.");

    string id = parameters["id"].ToString();

    if (!String.IsNullOrWhiteSpace(id)) 
        return String.Format("/{0}/{1}/{2}", controllerName, actionName, id);
    else
        return String.Format("/{0}/{1}", controllerName, actionName);
}
var url=GetUrlStringObjectArray=(string actionName、string controllerName、RouteValueDictionary参数)=>{
Assert.AreEqual(EventsController.Actions.Register.ToString(),actionName,“Url.Action被错误的操作调用”);
AreEqual(EventsController.ControllerName,ControllerName,“Url.Action使用不正确的控制器调用”);
字符串id=参数[“id”]。ToString();
如果(!String.IsNullOrWhiteSpace(id))
返回String.Format(“/{0}/{1}/{2}”,controllerName,actionName,id);
其他的
返回String.Format(“/{0}/{1}”,controllerName,actionName);
}
Edit:这里有一些关于CSharp中对象初始值设定项语法的附加资源

编辑(2): 我将留下上面的其他代码,但是如果您试图对使用UrlHelper扩展的东西进行单元测试,那就不那么容易了(尽管可以做到)。我不会在这里重新回答这个问题,但还有许多其他问题与此相关


我的同事帮助了我。这正是我想做的。这是一种匿名类型。使用反射,我可以得到一个对象的值


values.GetType().GetProperties()[0].GetValue(values,null)

您可能可以获取
dynamic
关键字来帮助实现这一点,但听起来您只是试图获得运行时生成的变量,这在C#中是不受支持的概念。你想做些别的事情吗?那么,你想去掉{}括号还是括号和“id=”?是否将此JSON数据传递到此方法?避免允许传入代码。相反,传入json,然后反序列化它。相关报道:也许是json,而我只是脑死亡。。但是有效的json应该是{“id”:“3”},对吗?我想这样做b/c这就是Url.Action helper在MVC中的工作方式它作为对象[]出现的事实闻起来很有趣。为什么不让这个方法使用RouteValueDictionary呢?这将分离出id和值(如果需要,可以将它们组合在一起)。这通常是MVC中路由的工作方式,路由数据有名称-值对。如果你能为你提供的代码添加更多的上下文,这会有所帮助。这似乎更正确。谢谢。这似乎是一个过于复杂(而且昂贵)的解决方案。如果这是生产代码,那么可能有一种更高效(可读性更好)的方法来实现您想要的而无需反射。你能提供关于你试图解决的问题的更多细节吗?xDaevax纠正了我。他有最好的答案。我试图为单元测试重新创建Url.Action方法。我已经用该场景的一些相关信息更新了我的答案。