After clarifying in comments that using FastMember is not a constraint I provide an answer using another library. I understand that you consider it a bit too broad, as its Reflection
API is just one of the several other features it offers. Its 'broadness' has a simple reason: this way the library has no 3rd party references at all, but it's still allowed even for its fairly specialized APIs (such as reflection or serialization) to use all of the core components like high performance collections, etc.
As for getting property accessors for the non-indexer properties only, the solution is simple: as PropertyAccessor
s can be obtained by regular PropertyInfo
instances you can simply perform the filtering on the actual properties:
var simpleProperties = type.GetTypeInfo().DeclaredProperties.Where(
p => p.CanRead // readable properties
&& !p.GetMethod.IsStatic // instance properties
&& p.GetMethod.GetParameters().Length == 0); // non-indexers
// getting the PropertyAccessors for them:
return simpleProperties.Select(pi => PropertyAccessor.GetAccessor(pi));
The approach of getting/setting properties is a bit different. FastMember expects accessing the properties by name, whereas my library provides PropertyInfo
-like accessors for each properties. In fact, you can also use the Reflector
class if you want to access the properties by name but that class is more about convenience rather than performance.
See this online example for some usage test and comparisons with FastMember.
While creating the example and tried FastMember I realized that both libraries have pros and cons. For example, non-generic access is faster by FastMember than by my library. On the other hand, FastMember supports instance public properties and fields only (even if you specify allowNonPublicAccessors: true
), whereas my library can access any kind of members of any visibility, including methods, constructors and static members.