Click here to Skip to main content
14,690,757 members
Please Sign up or sign in to vote.
4.83/5 (6 votes)
See more:
Ok, I am calling an interop dll which I have no access to. Here is the pseudo code:
dynamic myVariable = null;

firstInteropMethod(ref myVariable);
secondInteropMethod(myVariable); //Not by ref

The method signatures for the two methods are
firstInteropMethod(ref object someObject);
firstInteropMethod(object someObject);

The expected value is a double array of the definition

Now the fun part. My original code gets the wrong results but no error. However, this code:
firstInteropMethod(ref myVariable);
secondInteropMethod((double[,]) myVariable);

Gives the expected results.

Using watches and type of statements I have determined that nothing changes between the two calls so what gives? Why would there be a difference and what would that difference be?

In C#, all class objects are sent as reference by default and by this statement taken from MSDN documentation, my guess is that you are still sending by reference either way.

The type is a static type, but an object of type dynamic bypasses static type checking. In most cases, it functions like it has type object.

My best guess is that there is a cast attempt in the "ref" method that is failing because of the "object" reference.

This is a really interesting dilemma that I'm going to have to dig deeper into.
Ennis Ray Lynch, Jr. 5-Jan-11 18:05pm
My question is not about whether I am sending it by reference or not but why a simple cast makes all the difference in the world when by all documentation it should not.

And, IIRC, since I am using interop my parameters are not exactly passed by reference unless the method signature explicitly states otherwise due to marshaling yada, yada, yada.
1-Could you show the definition of the original methods?
2-What happens if you change dynamic to just object in the variable declaration?
3-You don't have secondInteropMethod overloaded, do you?
4-could you also elaborate on how the end value is wrong? is it unaffected by te first method or the second method? both? i sustect this could be a boxing issue.

This MSDN articleon dynamic explains why casting is needed for COM Interop when operations declare the parameter type as object and indicates that using the /link:filelist compiler option will allow you to define the COM method signatures as dynamic as well.

The article I cited was in response to why a cast is necessary. The specific line in the article (under the COM Interop heading) states: "Many COM methods allow for variation in argument types and return type by designating the types as object. This has necessitated explicit casting of the values to coordinate with strongly typed variables in C#."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900