javascript 如何在 C# 中将 ExpandoObject 转换为字典?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32618425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:31:51  来源:igfitidea点击:

How can I convert an ExpandoObject to Dictionary in C#?

javascriptc#dictionaryexpandoobject

提问by Chris Williams

I'm using Jint to execute JavaScript in a Xamarin app. Jint is converting an associative array into an ExpandoObject. How do I use this object? Ideally, I'd like to get a dictionary of the data out of it.

我正在使用 Jint 在 Xamarin 应用程序中执行 JavaScript。Jint 正在将关联数组转换为 ExpandoObject。我如何使用这个对象?理想情况下,我想从中获取数据字典。

JavaScript returns:

JavaScript 返回:

return {blah:abc, bleh:xyz};

Debugger of Object that Jint returns looks like:

Jint 返回的对象调试器如下所示:

enter image description here

在此处输入图片说明

回答by Zotta

It already IS a dictionary. Just implicitly cast it:

它已经是一本字典。只是隐式地投射它:

IDictionary<string, object> dictionary_object = expando_object;

And then use it like one. BTW: this is also the reason why recursive's solutionworks.

然后像一个一样使用它。顺便说一句:这也是递归解决方案有效的原因。

回答by recursive

Just pass it to the constructor.

只需将其传递给构造函数即可。

var dictionary = new Dictionary<string, object>(result);