I have a Person
class that inherits EntityBase
:
public class Person : EntityBase
{
virtual public string FirstName { get; set; }
virtual public string LastName { get; set; }
virtual public IList<Asset> Assets { get; set; }
}
And
public class EntityBase : IEntity
{
public virtual long Id { get; protected set; }
public virtual IEnumurable<string> Errors { get; protected set; }
}
I need to get list of properties of Person
class that is not collection
Now GetProperties()
is include : FirstName, LastName, Assets, Id, Errors
but I need only not array properties : FirstName, LastName, Id
How can I get the properties that are not collection ?
You can filter by the return type of the property. I suspect you want to filter out anything that implements IEnumerable
, but isn't string
(which implements IEnumerable<char>
, but which you want to keep). So something like:
var properties = type.GetProperties()
.Where(p => p.PropertyType == typeof(string) ||
!typeof(IEnumerable).IsAssignableFrom(p.PropertyType));
See more on this question at Stackoverflow