Collection of Objects how to find the prop in C# code example
Example 1: C# get object property name
using System.Reflection;
PropertyInfo[] propertyInfos;
propertyInfos = typeof(MyClass).GetProperties(BindingFlags.Public |
BindingFlags.Static);
Array.Sort(propertyInfos,
delegate(PropertyInfo propertyInfo1, PropertyInfo propertyInfo2)
{ return propertyInfo1.Name.CompareTo(propertyInfo2.Name); });
foreach (PropertyInfo propertyInfo in propertyInfos)
{
Console.WriteLine(propertyInfo.Name);
}
Example 2: check property type of collection c#
var obj = new PropClassDemo();
foreach (PropertyInfo prop in obj.GetType().GetProperties())
{
if(prop.PropertyType != typeof(string) && typeof(IEnumerable).IsAssignableFrom(prop.PropertyType))
{
Console.Writeline("This prop's type is Ienumerable");
}
}