I'm trying to get all public properties in the below type.
In .NET Framework I'd to that by using IsPublic
from the PropertyInfo
type but that does not seem to exist in .NET Core 2.
internal class TestViewModel
{
public string PropertyOne { get; set; }
public string PropertyTwo { get; set; }
}
//how can I retrieve an IEnumerable with PropertyOne and PropertyTwo ONLY?
var type = typeof(TestViewModel);
var properties = type.GetProperties().Where(p => /*p.IsPublic &&*/ !p.IsSpecialName);
An alternative is to use the PropertyType member, as such..
Programmer().GetType().GetProperties().Where(p => p.PropertyType.IsPublic && p.DeclaringType == typeof(Programmer));
public class Human
{
public int Age { get; set; }
}
public class Programmer : Human
{
public int YearsExperience { get; set; }
private string FavLanguage { get; set; }
}
This successfully returns only the public int YearsExperience.