Any .Net gurus here. Need help

CheekyC

Senior Member
Joined
Nov 25, 2013
Messages
515
Reaction score
0
I need help on DynamicMethod and Reflection.Emit to generate IL for compilation...

Anyone?
 
Oh thats piss easy. Read a single record and create the object in memory, assign it to a dynamic and then add to a list List<dynamic> and then just append to the list.

I wrote this a few weeks back and build collections of about half a million records and dont have slow downs.

Btw you can also use an ExpandoObject

dynamic obj = new System.Dynamic.ExpandoObject();
 
Last edited:
If I understand you correctly, what you want to use is a MicroORM library (there are many around, have a look at http://www.toptensoftware.com/petapoco/)

I've written such a micro ORM lib, but can't share it, the opensource libs are way more feature rich anyways.

That is such a bloated way of doing things. He can write a 30 line class to give him what he needs. Or just use the expandoobject
 
That is such a bloated way of doing things. He can write a 30 line class to give him what he needs. Or just use the expandoobject


Sure, its quick, clean and easy, but if performance is critical, then ExpandoObject is not the answer. MicroORM libraries come very close to hard-coded hand-tuned code. I don't know what the OP's requirements are, so ExpandoObject might very well fit the bill! If not, he can decide whether he would like to have a look at other suggested alternatives.
 
Well he can weave his own reflection emit code, its not actually difficult at all instead of bringing in an entire orm.
 
Well he can weave his own reflection emit code, its not actually difficult at all instead of bringing in an entire orm.

I have my own ORM framework because I wrote it MANY moons ago. Before EF and it also runs on Compact Framework. It is very lightweight.

Right at the lowest level it uses my ReflectionRecordReader class. This class iterates through the DataReader fields for the current row and sets the object properties from the DB columns using reflection. This is where the performance hit comes. My ORM framework is such that I can inject any record reader into the data access layer but the default is the reflection based reader.

I have done some perf tests. To open the DataReader and iterate through all rows without actually reading the row field values take 150ms for 1000 rows. Using my ReflectionRecordReader, it takes 4500ms. Factor of 20 times slower. The table has 180 columns.

The system is currently running on .NET Framework 2 only...

So yes I can weave my own reflection emit code (as in the CodeProject article) but that is where i need the help....
 
I have my own ORM framework because I wrote it MANY moons ago. Before EF and it also runs on Compact Framework. It is very lightweight.

Right at the lowest level it uses my ReflectionRecordReader class. This class iterates through the DataReader fields for the current row and sets the object properties from the DB columns using reflection. This is where the performance hit comes. My ORM framework is such that I can inject any record reader into the data access layer but the default is the reflection based reader.

I have done some perf tests. To open the DataReader and iterate through all rows without actually reading the row field values take 150ms for 1000 rows. Using my ReflectionRecordReader, it takes 4500ms. Factor of 20 times slower. The table has 180 columns.

The system is currently running on .NET Framework 2 only...

So yes I can weave my own reflection emit code (as in the CodeProject article) but that is where i need the help....

Ah okay, well i can put something together for you over the weekend.(Bit busy right now). If you can wait that long...
 
Are you caching the data column offsets, or do you find the index of a column with each iteration ? (makes a big difference wrt performance).

I assume you're using vanilla Reflection to set the values of Fields and Properties.
- A possible quick win would be to only use Fields
- Alternatively, have a look at this link, it basically shows you how to generate getters and setters for properties (using emit as you requested)
http://jachman.wordpress.com/2006/08/22/2000-faster-using-dynamic-method-calls/


Good luck.
 
Are you caching the data column offsets, or do you find the index of a column with each iteration ? (makes a big difference wrt performance).

I assume you're using vanilla Reflection to set the values of Fields and Properties.
- A possible quick win would be to only use Fields
- Alternatively, have a look at this link, it basically shows you how to generate getters and setters for properties (using emit as you requested)
http://jachman.wordpress.com/2006/08/22/2000-faster-using-dynamic-method-calls/


Good luck.

That's pretty much how i generated my fields.
 
Ah okay, well i can put something together for you over the weekend.(Bit busy right now). If you can wait that long...

Awesome man, appreciated.

The DAL class is a generic class that you instantiate with the type of the object to retrieve.

It uses reflection to build the database metadata (column names etc) from the object type. So it ends up with a mapping between
DB column name and object property name. It builds all the SQL statements from there. The record reader then uses this as input to set the property names from the DataReader fields. So you would need this class. PM me your email then I will send it to you. Some of the class properties can be nullable as well. the types I mostly use in my objects are

string
DateTime
byte
short
int
long
float
decimal
double
and their Nullable<T> versions

Lemme know if you have Q's
Tx
 
Awesome man, appreciated.

The DAL class is a generic class that you instantiate with the type of the object to retrieve.

It uses reflection to build the database metadata (column names etc) from the object type. So it ends up with a mapping between
DB column name and object property name. It builds all the SQL statements from there. The record reader then uses this as input to set the property names from the DataReader fields. So you would need this class. PM me your email then I will send it to you. Some of the class properties can be nullable as well. the types I mostly use in my objects are

string
DateTime
byte
short
int
long
float
decimal
double
and their Nullable<T> versions

Lemme know if you have Q's
Tx

I'll write a generic builder and then you can adapt it where required.
 
Are you caching the data column offsets, or do you find the index of a column with each iteration ? (makes a big difference wrt performance).

I assume you're using vanilla Reflection to set the values of Fields and Properties.
- A possible quick win would be to only use Fields
- Alternatively, have a look at this link, it basically shows you how to generate getters and setters for properties (using emit as you requested)
http://jachman.wordpress.com/2006/08/22/2000-faster-using-dynamic-method-calls/


Good luck.

Yes I cache the column index in the first row when get from cache on next rows. Coming to think of it.. I need to check. I might even have it in a static cache so that the cache is built for the object type only once during the app lifetime and not once per SELECT

Yes I use vanilla reflection with properties. Why would fields be quicker? Doesn't the optimiser expand the inline setters?
Will read the article tx
 
Top
Sign up to the MyBroadband newsletter
X