views:

311

answers:

1

Is it possible to call a .NET method from PowerShell with early binding?

I have a pipeline script which calls a single .NET method in process {...}. PowerShell calls this method via reflection, and right now Invoke (not the method itself, just reflection call) takes 70% of total execution time.

The method is always the same, so I would prefer to ask PowerShell not to use reflection at all.

+3  A: 

I believe the only types of method call that is early bound in PowerShell, or at least as early bound as is possible in a dynamic language, are the following

  1. CmdLets.
  2. Static Methods

I'm not as sure about #2. I believe they still have to use reflection to get at the underlying method.

CmdLets are likely the better choice here. In that case the actual call is bound early, but the parameters still must undergo a conversion process. Try moving your method call into a CmdLet and seeing if that helps you out.

JaredPar
Thanks for the answer. The problem is that if I go the CmdLet way, I'll have to create a C# project, and in this case it is easier to create a C# console app for the whole task.And I was hoping to get a single small script (just call a method from dll API for all entries in a csv file).
Andrey Shchekin
I'll wait for some time and then accept your answer if there are no workarounds.
Andrey Shchekin