Reputation: 83
I've got a web service made in C# (WCF) that uses a COM object made in visual basic 6.0. I've followed this way to cast the COM object and use the function i want. The big problem is that the visual basic .dll is constantly changing (because its being developed and adaptaded to the client) and the CLSID is constantly changing.
This is the code. Is simple, I just cast the COM and send to print all the pages:
Printer print = new Printer(); //Where I Cast the COM Library
print.AllPages = 1; //its a parameter but i edited it so you can understand
return print.PrintPdf();
How can i make this code adaptable to changes? Is there any way I can change my code so I dont have to delete the reference, re-add it and rebuild the proyect?
Thanks
Upvotes: 0
Views: 77
Reputation: 2124
If the COM-Object supports late binding (what, if I recall correctly, every VB6 COM-Object should support by default) you can create a Object without knowing the CLSID at compile-time via Activator.CreateInstance:
dynamic printer = Activator.CreateInstance(Type.GetTypeFromProgID("SisPrinter.Printer"));
printer.AllPages = 1;
printer.PrintfPdf();
Late binding is a slower than early binding (via adding a Reference in Visual Studio) but even allows you to call methods which were introduced in an later version of this COM-Object. It's main purpose is to enable support for scripting languages.
Instead of coding against your imported interfaces you would use the dynamic-Type which is available since C# 4.0. In terms of software-architecture this approach is not as good as adding a static reference but may help you until your interfaces are stable.
Upvotes: 1