Reflection pre-processing
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 4 of 4

Thread: Reflection pre-processing

  1. #1
    Join Date
    Apr 2010

    Reflection pre-processing


    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:


    class MyClass {

    int a, b, c;
    SomeObject someObject;
    AnotherObject anotherObject;

    public CopyTo(MyClass target){
    target.a = a;
    target.b = b;
    target.c = c;


    public Purge(){
    a = b = c = 0;

    someObject = null;
    anotherOject = null;



    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?

  2. #2
    Join Date
    Mar 2004
    Prague, Czech Republic, EU

    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.

  3. #3
    Join Date
    Mar 2004
    3311'18.10"N 9645'20.28"W

    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.

  4. #4
    Join Date
    May 2007

    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:

    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? 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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)