获取动态类型的属性 [英] Get properties of a Dynamic Type
问题描述
我想知道如何获取动态类型的属性。
I would like to know how to get the properties of my dynamic type.
这是获取列表的功能,
var result = _files.GetFileContent(reportId).Result;
例如,我得到一个这样返回的对象:
As example I get an object returned like this :
当我打开它时,您会看到我拥有的属性:
When I open it, you can see the properties I have :
我的想法是我永远都不知道这些属性。它们可以随着时间而改变。所以我想要一个包含所有属性的列表。因此,我可以动态地使用它们。
The Idea is that I never know the properties. They can change over time. So I want a list which is filled with all the properties. So I can dynamically use them.
如何从第一项(ChargesDelta_DIFF_5,ChargesEfile_RIGHT,ChargesGecep_LEFT等)中获取属性?
How Can I get the properties from the first item (ChargesDelta_DIFF_5, ChargesEfile_RIGHT,ChargesGecep_LEFT, etc)?
推荐答案
您可以使用反射来获取属性并将其转换为字典:
You can use reflection to get the properties out and convert it to a dictionary:
dynamic v = new { A = "a" };
Dictionary<string, object> values = ((object)v)
.GetType()
.GetProperties()
.ToDictionary(p => p.Name, p => p.GetValue(v));
这篇关于获取动态类型的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!