c# COM Objects issue

2019-07-25 14:19发布

问题:

Background

I am working on a trading ActiveX API in visual studio 2010 on C#. Since it is an ActiveX API, I simply added the ActiveX as my reference. The api provides three group of things: Method you could use to call API, the Event with which the API updates information for you and some socalled ActiveX COM object.

ISSUE

I asked a related question here: C# boolean int conversion issue Finally, after viewing the exception has been thrown, I know that it is about casting. Here is the description of exception:

*System.InvalidCastException was unhandled Message=Unable to cast object of type  
InteractiveBrokersTradingSystem.Forex' to type 'TWSLib.IContract'.

*

And here is my definition for Forex Class:

namespace InteractiveBrokersTradingSystem
{
  class Forex:TWSLib.IContract
  {


    public int conId { get; set; }
    public string symbol { get; set; }
    public string secType { get; set; }
    public string expiry { get; set; }
    public double strike { get; set; }
    public string right { get; set; }
    public string multiplier { get; set; }
    public string exchange { get; set; }
    public string primaryExchange { get; set; }
    public string currency { get; set; }
    public string localSymbol { get; set; }
    public int includeExpired { get; set; }
    public object comboLegs { get; set; }
    public object underComp { get; set; }
    public string comboLegsDescrip { get;set; }
    public string secIdType { get; set; }
    public string secId { get; set; }

    public Forex(string preCurrency,string baseCurrency)
    {
        //conId = 0;
        //symbol = preCurrency;
        //secType = "CASH";
        //expiry = null;
        //strike = double.Parse("0");
        //right = null;
        //multiplier = null;
        //exchange = "IDEALPRO";
        //primaryExchange = "IDEALPRO";
        //currency = baseCurrency;
        //localSymbol = null;
        //includeExpired = 0;
       // comboLegs = null;
        //underComp = null;
        //comboLegsDescrip = null;
        //secType = null;
        //secId = null;


      }
   }
 }

You could see that I did not assign any value to the properties of the class and the exception is always the same no matter what kind of value I assign or not assign or null.

In the description of the api below as image we can see that some property with () like strike() as doulble and some not with () like secType as string; someone told me that it might be problem. Please, give me any hint related to this COM casting issue:

a busy cat http://i.minus.com/iNIuzObDnHLJE.jpg

a busy cat http://i.minus.com/iKNApem2KXr6a.jpg

a busy cat http://i.minus.com/ib2ijVI3yGHc7H.jpg

回答1:

Can you post some code that actually fails?

I think what's happening is that you're trying to cast a COM object to a plain .NET type and that will not work because the COM object needs to explicitly be mapped to a .NET type either by the Runtime Callable Wrapper or by manual mapping.

One suggestion I would have is to skip explicit casting and use the dynamic type in .NET 4.0 to access the properties of your COM object, then map it to whatever properties/objects you need it to. That way you'll be able to see exactly which part of the object doesn't want to map.



标签: c# com casting