I have a class:
public class foo
{
public IEnumerable stst_soldToALTKN { get; set; }
public int sId { get; set; }
public strin
You can use this:
public IEnumerable<string> GetAllPropertyNames(object o)
{
foreach (PropertyInfo propInfo in o.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
yield return propInfo.Name;
}
You can use reflection to get a list of the properties and from them, select the name:
var foo1 = new foo();
var propertyNames = foo1.GetType()
.GetProperties(BindingFlags.Public | BindingFlag.Instance)
.Select(p => p.Name)
.ToList();
propertyNames
will now be a List<string>
.
BTW, you don't need an instance of foo
for this to work. You can get its type instead by doing:
var propertyNames = typeof(foo)
.GetProperties(BindingFlags.Public | BindingFlag.Instance)
.Select(p => p.Name)
.ToList();
var propNames = foo1.GetType()
.GetProperties()
.Select(pi => pi.Name)
You could try
var propertyNames = foo1.GetType()
.GetProperties()
.Select(x => x.Name).ToList();