views:

148

answers:

1

I've seen quite a few questions related to how do I invoke a method like this and that. What I haven't found is a listing of the different options of how to invoke a method via reflection or any other means in csharp.

Can someone explain in detail the different ways of dynamically invoking a method in csharp? From reflection to emitting IL and any other ways in between. I would like to know of all the different ways from most expensive to least expensive in terms of resources.

+5  A: 

To get you started, here are the ways to invoke a method in .NET that I can think of:

  • Call
  • Callvirt
  • Delegate
  • DynamicMethod
  • MethodInfo.Invoke
  • Type.InvokeMember
  • TypeDescriptor
  • Reflection.Emit
  • Expression Trees

I remember some article comparing the speed of most of them, but I can't seem to find it at the moment.

A quick Google search came up with these links: [Link] [Link] [Link] [Link] [Link]

dtb
can you provide code snippets of the most popular ones? Are these arranged by performance cost?
Sean Chambers
And of course, the "dynamic" feature in C# 4 uses all of the above. :-)
Eric Lippert