C# dynamic object and case sensitivity of method names and ditto arguments

EuroEager2008 171 Reputation points
2022-03-16T11:41:35.913+00:00

I am using a COM object which often changes the dispatch interface and thus introduce breaking changes.
I have tried switching to using the dynamic keyword and Activator.CreateInstance(Type type) with type from Type.GetTypeFromProgID(string progID).

This seems to work just fine and solves some of the versioning problems of the dispatch interface.
(The object has a fairly simple interface for which lack of intellisense and compile time check doesn't really matter that much, speed is also excellent after the first call (which caches lots of metadata)).

Due to the risk of added arguments to methods of the COM object, I call the methods with named arguments and trust the issuer of the COM object to add arguments with default values.

Then I got a version of the COM object where one method name changed case and similarly one argument name of the method changed case, e.g. Testmethod(string Testargument) changed to TestMethod(string TestArgument).
The calls look like this (myObject is declared as dynamic): var result = myObject.Testmethod(Testargument: "hello") and ditto var result = myObject.TestMethod(TestArgument: "hello").
I then realized that both methodnames and argument (parameter) names are case-insensitive when called by a dynamic object, which obviously fits my case perfectly.

My question is then: Is this case-insensitivity stable by design (and documented? where?) or is there a risk that this may change in the future?

Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. EuroEager2008 171 Reputation points
    2022-03-17T08:02:14.597+00:00

    Thanks
    This seems to be a bit messy.
    I can repro your exception easily, either using .net 5 or .net 6 (probably core 3.1, .net 4.0 (C# 4) etc. as well)

    My app was running .Net 6 (with top-level statements which doesn't seem to matter)
    Downgrading to .Net Core 3.1 shows same message as yours in a similar case (argument name with "wrong" case)

    Using .Net 5 (C# 9 compiler) or .Net 6 (C# 10 compiler) does not show any error in my case.

    The principle differences as I can see are:
    1: Your program creates the type by typeof(ExampleClass), mine by Type.GetTypeFromProgID("MyProgID")
    2: Your program uses a .net class (cls)

    Perhaps the fact that you actually introduce the ExampleClass to the compiler makes the difference.
    (The compiler has the possibility to add some metadata at compiletime even if dynamic is used, in my case with totally unknown metadata at compile time everything has to be done and cached in runtime)

    Based on the observations I have to rephrase my question:
    Is this case-insensitivity, using C#9 or C#10 and COM interop by the dynamic keyword stable by design (and documented? where?) or is there a risk that this may change in the future?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.