Reputation: 23833
I have the following code that instantiates a registered COM object
private dynamic server = null // Global variable.
...
Type type = Type.GetTypeFromProgID("Svr.GrpCall");
this.server = Activator.CreateInstance(type);
I can invoke methods from this COM/dynamic
object just fine using very intuitive notation
string expectedResult = this.server.GroupInit(someString, someBoolean);
For single calls the performance of calling such methods through the Dynamic Language Runtime (DLR) is liveable. However, for one requirement I need to use a double for
loop for thousands of records, which uses a method in the COM object that returns void
for (int i = 0; i < MAXREC; i++)
for (int j = 0; j < MAXFIELDS; j++)
this.server.MakeRecord(s1, s2, str);
This is MASSIVELY slow. I am aware of this and Eric Lippert's answer https://stackoverflow.com/a/7480977/626442 gives the exact cause of this performance bottle-neck.
To get around this I have attempted to define a global Action
for the MakeRecord
method that is under pressure
private Action<short, short, string> makeRecord;
where the plan is to use this in the for loop above. Before using this Action
, I attempt to first initialise the server (as above) and then assign the makeRecord
delegate to the COM method using
Type type = Type.GetTypeFromProgID("Svr.GrpCall");
this.server = Activator.CreateInstance(type);
MethodInfo methodInfo = type.GetMethod("MakeRecord");
makeRecord = (Action<short, short, string>)Delegate.CreateDelegate(type, methodInfo);
But the type.GetMethod("MakeRecord")
is returning null
. So, my question is,
If this IS the right methodology to speed up my dynamic
calls, how can I assign the COM method MakeRecord
to my Action
delegate?
If this IS NOT the right methodology to speed up my dynamic
calls, what is?
Thanks for your time.
I have also tried doing
object o = Activator.CreateInstance(type);
MethodInfo methodInfo = o.GetType().GetMethod("MakeRec");
Upvotes: 1
Views: 350
Reputation: 99859
How do you know the slow behavior is due to the use of dynamic
?
dynamic
and without using any reflection methods)? You can add a reference to the COM assembly itself to create the managed wrapper automatically.Upvotes: 2