I'm making a helper method where it would automatically set random values to a property of a given entity (class) so that I won't have to fill each property with a value when testing.
In my case, each entity inherits from BaseEntity class, which has the ID, CreatedBy, CreatedOn, etc... properties. basically this class has all the properties that are shared across all the entities.
What I'm trying to accomplish here is to separate the unique properties from the common ones.
Here's my code:
public static TEntity PopulateProperties<TEntity>(TEntity entity)
{
try
{
// Since every entity inherits from EntityBase, there is no need to populate properties that are in EntityBase class
// because the Core project populates them.
// First of all, we need to get all the properties of EntityBase
// and then exlude them from the list of properties we will automatically populate
// Get all properties of EntityBase
EntityBase entityBase = new EntityBase();
List<PropertyInfo> entityBaseProperties = new List<PropertyInfo>();
foreach (var property in entityBase.GetType().GetProperties())
{
entityBaseProperties.Add(property);
}
// Get all properties of our entity
List<PropertyInfo> ourEntityProperties = new List<PropertyInfo>();
foreach (var property in entity.GetType().GetProperties())
{
ourEntityProperties.Add(property);
}
// Get only the properties related to our entity
var propertiesToPopulate = ourEntityProperties.Except(entityBaseProperties).ToList();
// Now we can loop throught the properties and set values to each property
foreach (var property in propertiesToPopulate)
{
// Switch statement can't be used in this case, so we will use the if clause
if (property.PropertyType == typeof(string))
{
property.SetValue(entity, "GeneratedString");
}
else if (property.PropertyType == typeof(int))
{
property.SetValue(entity, 1994);
}
}
return entity;
}
finally
{
}
}
The propeblem is with var propertiesToPopulate = entityBaseProperties.Except(ourEntityProperties).ToList();
What I'm expecting is a list of PropertyInfo objects that are only unique to this entity, however I'm always getting ALL the properties of my entity. This line does not filter the list as intended.
Any help why??
PropertyInfo
"knows" which type you used to ask for it. For example:
using System;
using System.Reflection;
class Base
{
public int Foo { get; set; }
}
class Child : Base
{
}
class Test
{
static void Main()
{
var baseProp = typeof(Base).GetProperty("Foo");
var childProp = typeof(Child).GetProperty("Foo");
Console.WriteLine(baseProp.Equals(childProp));
Console.WriteLine(baseProp.ReflectedType);
Console.WriteLine(childProp.ReflectedType);
}
}
has output of:
False
Base
Child
Fortunately, you can do this a lot more simply - if you just want to know which properties were declared in TEntity
you can just use:
var props = typeof(entity.GetType()).GetProperties(BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.DeclaredOnly);
Adjust if you want static properties as well. The important point is BindingFlags.DeclaredOnly
.
See more on this question at Stackoverflow