Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. MethodInfo Invoke, bad performance?

MethodInfo Invoke, bad performance?

Scheduled Pinned Locked Moved C#
graphicsperformancequestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    STW
    wrote on last edited by
    #1

    I use the MethodInfo Invoke() Method. The Invoked Method is a drawing routine. Is it right that the MethodInfo Invoke() Method is extremly slow?

    E D 2 Replies Last reply
    0
    • S STW

      I use the MethodInfo Invoke() Method. The Invoked Method is a drawing routine. Is it right that the MethodInfo Invoke() Method is extremly slow?

      E Offline
      E Offline
      Eric Gunnerson msft
      wrote on last edited by
      #2

      Compared to a direct call, yes, it's very slow. What are you trying to do, and can you do it with an interface? Making calls through interfaces is fast.

      S 1 Reply Last reply
      0
      • S STW

        I use the MethodInfo Invoke() Method. The Invoked Method is a drawing routine. Is it right that the MethodInfo Invoke() Method is extremly slow?

        D Offline
        D Offline
        Daniel Turini
        wrote on last edited by
        #3

        STW wrote: I use the MethodInfo Invoke() Method. The Invoked Method is a drawing routine. Is it right that the MethodInfo Invoke() Method is extremly slow? Someone once said that if you need to ask how big is the cost of Reflection, you can't afford it. But there are much faster alternatives, like interfaces or delegates. Acting as a substitute for God, he becomes a dispenser of justice. - Alexandre Dumas

        S 1 Reply Last reply
        0
        • E Eric Gunnerson msft

          Compared to a direct call, yes, it's very slow. What are you trying to do, and can you do it with an interface? Making calls through interfaces is fast.

          S Offline
          S Offline
          STW
          wrote on last edited by
          #4

          I thought I had an "extremly cunning plan" but hope you can tell me a better solution: I'm programming a little game with a lot of UserControls. These UserControls are filled with Data. But I don't like to pass DataAccess references to all of these Controls, so I thought I do it like in C++. MFC C++ Prgs are splitted in UI Views and Documents. Each View can have a document. From this document it's very simple to update the views (GetDocument().UpdateView(Name)) with new data. In this scenario I wouldn't have to pass references to the DataSet; I just have to programm a document class which handles adding Document Items (Controls, which are "listening" to changes of the document) and the Controls Update Methods. I made it like this: object array for the document items String array for the Update Method Name of the Control DocumentType, so I can Update only specific controls. MyDocument.AddDocumentItem(this.AreaControl1, "AreaCtrlUpdate", DocumentTypes.AreaControl) The Document has a DataSet and properties. So when the documents data changed with the set method of a property this will Update my Document Items. To Invoke a Method of a object I needed MethodInfo.Invoke() Method. Each DocumentItem have to implement a method like CtrlUpdate(Document pDoc). I add as a passing parameter of the CtrlUpdate method the Document itsself. What I found very good was first that Data is only stored at one place (in the document) and there is even no need to store IDs in the UserControls. Second there's no need to pass DataSet or DataAccess Object References because the DataSet is passed with the Document to the Controls Update Method. In the document I fix which Controls to Update if a property or the dataset changed. That's good because I can fix which Controls to update at only one place, in the document and there's no need that a control has object references to other controls which have to actualisize when the data was changed by the control. I just set the new property in the document and this will cause the Update Method of the other Controls. I made a mistake and so I meant that MethodInfo.Invoke() is very slow (Seconds to Invoke). So what do you mean with calling Methods through an interface? Is there a better solution for this scenario? Could you give me an example? I think about to use databinding with these UserControls but I couldn't find examples how to set a datasource, datamember for a UserControl. Do you know how to set datasource of usercontrols? Thank you! Stefan

          E 1 Reply Last reply
          0
          • D Daniel Turini

            STW wrote: I use the MethodInfo Invoke() Method. The Invoked Method is a drawing routine. Is it right that the MethodInfo Invoke() Method is extremly slow? Someone once said that if you need to ask how big is the cost of Reflection, you can't afford it. But there are much faster alternatives, like interfaces or delegates. Acting as a substitute for God, he becomes a dispenser of justice. - Alexandre Dumas

            S Offline
            S Offline
            STW
            wrote on last edited by
            #5

            I explained my scenario to Eric Gunnerson. Hope you could help me to find a faster solution with interfaces. Thank you! Stefan

            1 Reply Last reply
            0
            • S STW

              I thought I had an "extremly cunning plan" but hope you can tell me a better solution: I'm programming a little game with a lot of UserControls. These UserControls are filled with Data. But I don't like to pass DataAccess references to all of these Controls, so I thought I do it like in C++. MFC C++ Prgs are splitted in UI Views and Documents. Each View can have a document. From this document it's very simple to update the views (GetDocument().UpdateView(Name)) with new data. In this scenario I wouldn't have to pass references to the DataSet; I just have to programm a document class which handles adding Document Items (Controls, which are "listening" to changes of the document) and the Controls Update Methods. I made it like this: object array for the document items String array for the Update Method Name of the Control DocumentType, so I can Update only specific controls. MyDocument.AddDocumentItem(this.AreaControl1, "AreaCtrlUpdate", DocumentTypes.AreaControl) The Document has a DataSet and properties. So when the documents data changed with the set method of a property this will Update my Document Items. To Invoke a Method of a object I needed MethodInfo.Invoke() Method. Each DocumentItem have to implement a method like CtrlUpdate(Document pDoc). I add as a passing parameter of the CtrlUpdate method the Document itsself. What I found very good was first that Data is only stored at one place (in the document) and there is even no need to store IDs in the UserControls. Second there's no need to pass DataSet or DataAccess Object References because the DataSet is passed with the Document to the Controls Update Method. In the document I fix which Controls to Update if a property or the dataset changed. That's good because I can fix which Controls to update at only one place, in the document and there's no need that a control has object references to other controls which have to actualisize when the data was changed by the control. I just set the new property in the document and this will cause the Update Method of the other Controls. I made a mistake and so I meant that MethodInfo.Invoke() is very slow (Seconds to Invoke). So what do you mean with calling Methods through an interface? Is there a better solution for this scenario? Could you give me an example? I think about to use databinding with these UserControls but I couldn't find examples how to set a datasource, datamember for a UserControl. Do you know how to set datasource of usercontrols? Thank you! Stefan

              E Offline
              E Offline
              Eric Gunnerson msft
              wrote on last edited by
              #6

              Here's what I suggest: Define an interface: interface IDocumentItem { void CtrlUpdate(Document pDoc); } and then implement it in each of the DocumentItem classes. Then, when you want to do the update, you can cast the DocumentItem to an IDocumentItem, and then call CtrlUpdate() through that reference. That will be fast fast. Does that make sense?

              S 1 Reply Last reply
              0
              • E Eric Gunnerson msft

                Here's what I suggest: Define an interface: interface IDocumentItem { void CtrlUpdate(Document pDoc); } and then implement it in each of the DocumentItem classes. Then, when you want to do the update, you can cast the DocumentItem to an IDocumentItem, and then call CtrlUpdate() through that reference. That will be fast fast. Does that make sense?

                S Offline
                S Offline
                STW
                wrote on last edited by
                #7

                This does really make sense! Yes that's what I searched. The problem was always that a casted object "looses" the classes methods...So now I see... Thank you very much! Stefan

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups