Tuesday, April 5, 2011

How do you call a com object method from c# 4.0.

The API documents describe the method as such:

boolean MethodName(VARIANT* par)

The parameter is a ref type which returns an error code.

How do I call this method using c# 4.0's new features?

From stackoverflow
  • Just use the old way, there's nothing in 4.0 that makes this method any easier to use. Add a reference to the COM type library, usually the DLL itself, and you should get a class with the method bool MethodName(ref object). What you are supposed to do with the object is completely unclear from your question. Check the API manual, get help from the component vendor.

    Behrooz Karjooravary : i need to cast the object to a string. that's why i was wondering if the new dynamic or var types would be useful. I will experiment some more to see if I can just cast it to string without them.
    Behrooz Karjooravary : would there be any benefit if I pass in a dynamic or var type instead of object?
    Hans Passant : There is no such thing as a 'var type', type inference is not a runtime feature. Whether it is declared *dynamic* or *object* doesn't matter, you still have to cast it. Just use object and cast it after the call.
    Behrooz Karjooravary : Object casted to string with no problem. Thanks

0 comments:

Post a Comment