0

I am interested in the possibilities offered by the C# dynamic keyword, but I am facing unexpected issues.

If I create a default .NET 4.8 template in Visual Studio 2022...

This works:

dynamic obj = Marshal.GetActiveObject("Excel.Application");
obj.Workbooks.Add();

This works too:

object obj1 = Marshal.GetActiveObject("FancySoft.Application");
object obj2 = obj1.GetType().InvokeMember("FancyProperty", BindingFlags.GetProperty, null, obj1, null);

This fails with a System.Runtime.InteropServices.COMException: 'Error HRESULT E_FAIL has been returned from a call to a COM component.':

dynamic obj1 = Marshal.GetActiveObject("FancySoft.Application");
object obj2 = obj1.FancyProperty;

Finally, if I add a COM reference to FancySoft to my project, this works as expected:

var obj1 = (Application) Marshal.GetActiveObject("FancySoft.Application");
var obj2 = obj1.FancyProperty;

I have found an interesting discussion thread that suggests that a Vendor can intentionally restrict the use of the dynamic keyword in client applications.

In my case, the Vendor doesn't even officially support .NET-based automation scenario (although it is a common scenario in our industry) so I can't ask them for that type of information.

Have you seen this behavior elsewhere? Am I missing something? Is there any way to confirm that this behavior is intentional without asking the vendor? (By looking at the .TLB files, using some reflection tricks, etc.?)

5
  • Here is a comprehensive doc to handle your issues. learn.microsoft.com/en-us/dotnet/framework/interop/… Commented Nov 16, 2023 at 17:26
  • 1
    dynamic mostly (it not entirely) uses IDispatch ("aka late binding" which is name-based), and yes this is completely possible that it doesn't work. I doubt it's intentional, more a bad interface design, as demonstrated in other post stackoverflow.com/questions/77478817/… Commented Nov 16, 2023 at 17:50
  • @SimonMourier I wrote a minimal vbscript tonight, which works as expected with the Name property from that other post. That should too be using IDispatch right? If so why a different behavior? Commented Nov 16, 2023 at 22:55
  • 1
    Well vbscript internals are not the same as .net internals, there are many ways to use IDispatch. Difficult to say more w/o a reproducing COM object to test this specific IDispatch by myself Commented Nov 17, 2023 at 0:04
  • Thanks @SimonMourier, I learn a lot with every answer. Unfortunately it is highly unlikely that you have access to this program. I just tried the same example in a VB.NET project, and the property can be accessed as one would expect (regardless whether obj1 is defined as an Object or an Application). I would have thought the behavior in VB would be closer to C#, but that's apparently not the case. Commented Nov 17, 2023 at 11:17

1 Answer 1

0

The vendor fixed (at least in part) their COM implementation in a recent release: the dynamic keyword now works as expected. So does the dynamic view when debugging the COM objects.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.