Search code examples
c#.netdynamiccomole

How do I set a function property on a dynamic COM object?


I am looking at Amibroker's OLE documentation examples in VBScript and JS trying to convert it to C# code: http://www.amibroker.de/guide/objects.html

In it it says:

Filter( 0, "index" ) = 1; // include only indices
Filter( 1, "market" ) = 2; // exclude 2nd market

I have a C# dynamic object that I built, and I can find and call the Filter() function, but I have no idea how to set the value after the function call, since that is not valid C# syntax.

Here is the C# code:

var type = Type.GetTypeFromProgID("Broker.Application");
dynamic ab = Activator.CreateInstance(type);
ab.Analysis.Filter(0, "market") = 2; // This is obviously not compiling

When I call ab.Analysis.Filter(0, "market"), it simply returns an int for the current setting. Is the answer to use reflection somehow? I haven't tried to go down that route wondering if there is a simpler solution.


Solution

  • That code snippet you found is jscript, not VBScript. It is not a function property, it is an indexed property. VB.NET supports them well. But the C# team did not like them and only permits one indexed property for a class, the indexer (this[]). By popular demand they added support in version 4. Only for COM interop. Which is what you are using.

    Just like the indexer, you use square brackets for indexed properties:

     AA.Filter[0, "market"] = 1;
    

    Which should be supported by dynamic as well. Explicitly calling the setter function would be another way, AA.set_Filter(0, "market", 1).

    Note that you'll have a much easier time writing this code when you add a reference to the type library. That lights up IntelliSense and the red squiggles.