-
April 30th, 2010, 04:58 AM
#1
Reflection pre-processing
Hi,
I'd like to use reflection, however performace issues are stopping me. Most of my reflection needs are static; the real motivation would be to save me writing as much code.
So for example, suppose I have a class:
<code>
class MyClass {
int a, b, c;
SomeObject someObject;
AnotherObject anotherObject;
public CopyTo(MyClass target){
target.a = a;
target.b = b;
target.c = c;
target.someObject.CopyTo(target.someObject);
target.anotherObject.CopyTo(target.anotherObject);
}
public Purge(){
a = b = c = 0;
someObject = null;
anotherOject = null;
}
}
</code>
Now, if I were to implement CopyTo using reflection to iterate over, say, each integer attribute, of the class type, it would be hellishly slow - and this information is static anyway.
Another option would be to have every attribute implement some common interface, add every element to a list, and iterate over this.. but again this is introducing unecessary overhead.
What I would ideally like is some way to do this pre-compile time.
Any ideas?
-
May 3rd, 2010, 01:37 AM
#2
Re: Reflection pre-processing
I think dynamics from .NET 4.0 could help you, because as far as I know they uses dynamically created and compiled code at runtime.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
-
May 3rd, 2010, 09:09 PM
#3
Re: Reflection pre-processing
I'd suggest using an interface design if pure reflection is too slow (think IConvertible).
there are a number of ways you can speed up the reflection process. one way would be to cache the object type & member/property/method info's so that you only end up reflecting on the object once, and subsequent requests for that type use the cached ___Info's instead of finding them every time you need it.
-
May 4th, 2010, 03:43 AM
#4
Re: Reflection pre-processing
Or avoid the reflection overhead altogether, call "this.MemberwiseClone ()". That does exactly as you describe.
An alternative (possibly slower than MemberwiseClone) would be to use expression trees to generate a Func<T, T> which essentially does:
Code:
public void T Copy (T o)
{
T t = new T ();
t.FieldOne = o.FieldOne;
t.FieldTwo = o.FieldTwo;
...
...
return t;
}
I wrote code to do this before which worked out quite nicely. Using this method you do the reflection overhead once and then every subsequent copy just has to look up the cached generated code and away you go. The generated code will be as fast as if you had actually handcoded the Copy method for each and every type. Not too bad, eh?
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|