We have a class library that performs some basic operations like ORM, basically we can do something like this:
conn.Query<EntityType>("select * from table");
and return a List<EntityType>. Since mapping column names to properties of an entity type is stored inside an entity type, we can easily construct basic objects when we need only a subset of properties.
The problem is that this code is highly dependent on reflection, and we just found that the overhead we knew was much more than we expected.
example of timings of various operations related to "select * from table":
- IDataReader, iterate over all lines, 178ms
- IDataReader, call GetValues to get an object [] array for all rows, 260 ms
- IDataReader, calling GetValues, creating a new object and adding to the list for all lines, 356ms
- IDataReader, calling GetValues, creating a new object, using reflection to copy values to properties , ~ 10.500 ms ( 29x step 3. )
We cache reflection, but there is still too much overhead.
Since 99% of the objects involved are simple properties with (1 or 2 support fields), I thought I could just generate the following code via IL / DynamicMethod:
instance._Id = (int)value;
This does not work with FieldAccessException, and I assume that this is because the fields are private. Is there any way for me to generate this code at all, or am I just barking the wrong security tree?
source
share