dynamic objectDetails = new { Name="Ravindra Naik",Age=26,Gender="Male"};
//Now to fetch all the properties present in “objectDetails” here is what you need to do:
var properties = objectDetails.GetType().GetProperties();
//In order to fetch a list of property name and values, here is what you need to do:
foreach(var prop in properties)
{
var propName = prop.Name;
var propValue = objectDetails[propName];
}
枚举dynamic属性
最新推荐文章于 2025-06-06 12:39:44 发布
这篇博客介绍了如何在C#中动态地获取并遍历对象的属性。通过`GetType().GetProperties()`方法可以获取对象的所有属性,然后使用foreach循环结合反射技术,能够分别获取属性名和对应的值,这对于处理未知类型的数据非常有用。
1万+

被折叠的 条评论
为什么被折叠?



