How to

enumerate properties of anonymous type

Published: 21. December 2008 | Updated: 26. October 2011
License: Microsoft Public License (MS-PL)
Categories: Framework » Reflection, Framework » Types
Tags: C# Reflection
Was this snippet helpful for you? YESYES / NONO

Import namespace

using System.ComponentModel;

Code

object values = new { firstProperty = "value", secondProperty = 2 };

foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(values))
{
    Console.WriteLine("{0} = {1}", descriptor.Name, descriptor.GetValue(values));
}
Console Output:
firstProperty = value
secondProperty = 2
Send us feedback about this snippet »



Related Snippets: