开发者

Passing Key-Value pair to a COM method from .NET

开发者 https://www.devze.com 2022-12-26 11:00 出处:网络
I have the following problem: I have a project in C# where I use a third party COM component. So the problem is that the above component has a method, which takes a string and a number of key-value p

I have the following problem: I have a project in C# where I use a third party COM component.

So the problem is that the above component has a method, which takes a string and a number of key-value pairs as arguments. I already managed to call that method through JavaScript like that:

 var srcName
 srcName = top.cadView.addSource( 'Database', { driver : 'Oracle', host : '10.10.1.123', port : 1234, database : 'someSID', user : 'someuser', password : 'somepass' } )
 if ( srcName != '' )
 {
  ...
 }

... and it worked perfectly well. However I have no idea how to do the same using C#. I tried passing the pairs as Dictionary and Struct/Class but it throws me a "Specified cast is not valid." exception. I also tried using Hashtable like that:

开发者_如何学编程
Hashtable args = new Hashtable();
args.Add("driver", "Oracle");
args.Add("host", "10.10.1.123");
args.Add("port", 1234);
args.Add("database", "someSID");
args.Add("user", "someUser");
args.Add("password", "samePass");

String srcName = axCVCanvas.addSource("Database", args);

and although it doesn't throw an exception it still won't do the job, writing me in a log file

[ Error] [14:38:33.281] Cad::SourceDB::SourceDB(): missing parameter 'driver'


Could you pass in a list of delimited strings? Something like

List args = new List()

args.Add("key:value);

e.g. args.Add("driver:Oracle);


The ACtiveX component is probably using IDispatch or IDispatchEx methods to convert the dynamic type into a list of property names, and then accessing them.

Either find a different type to pass (which will also work) via the vendor or documentation, or you are going to need to implement one of those interfaces yourself (System.Collections.Dictionary doesn't).

Another thing to try is the scripting Dictionary type (you will need to import the correct typelib to get to that type).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号