Converting dynamic type to dictionary C#

前端 未结 7 1268
执念已碎
执念已碎 2021-01-01 11:22

I have a dynamic object that looks like this,

 {
    \"2\" : \"foo\",
    \"5\" : \"bar\",
    \"8\" : \"foobar\"
 }

How can I convert this

7条回答
  •  清酒与你
    2021-01-01 12:06

    You can use a RouteValueDictionary to convert a C# object to a dictionary. See: RouteValueDictionary Class - MSDN. It converts object properties to key-value pairs.

    Use it like this:

    var toBeConverted = new {
        foo = 2,
        bar = 5,
        foobar = 8
    };
    
    var result = new RouteValueDictionary(toBeConverted);
    

提交回复
热议问题