Basic reflection caching library and reflection powered object visitor.
Before:
var properties = target.GetType().GetProperties()
.Where(p => p.CanRead
&& !p.PropertyType.IsPointer
&& p.GetIndexParameters().Length == 0);
foreach (var prop in properties)
{
var val = prop.GetValue(target);
}
var _reflectiveCache = new ReflectiveCache();
var properties = _reflectiveCache.Get(target.GetType()).GetProperties()
.Where(p => p.CanRead
&& !p.PropertyType.IsPointer
&& p.GetIndexParameters().Length == 0);
foreach (var prop in properties)
{
var val = prop.GetValue(target);
}
- Type.GetProperties()
- PropertyInfo.GetValue()
- PropertyInfo.SetValue()
- PropertyInfo.GetIndexParameters()
All other methods just use the underlying version of the code so provide no performance improvement.
This library also includes a ReflectivePropertyVisitor
useful for traversing an objects parameters and applying a function on matching types.
The following example will execute the provided func on every DataStatusItem
hit during tree traversal.
var visitor = new ReflectivePropertyVisitor<DataStatusItem>(x =>
{
// do something
return Task.CompletedTask;
});
await visitor.VisitAsync(testObject);
See Reflective.Tests/ReflectivePropertyVisitorTests.cs
for an example.
The reason for this library is to provide performance improvements traversing an object using reflection - as such not all methods are cached, just those used in ReflectivePropertyVisitor
. Further functionality may be added in the future.