c#COM对象问题 [英] c# COM Objects issue
问题描述
背景
Background
我正在C#上使用Visual Studio 2010交易ActiveX API。由于它是一个ActiveX API,我只是添加了ActiveX作为我的参考。 api提供了三组事情:你可以用来调用API的方法,API为你更新信息的事件以及一些所谓的ActiveX COM对象。
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
ISSUE
这里: C#boolean int conversion 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'.
*
外汇类:
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;
}
}
}
你可以看到,我没有赋予类的属性的任何值,并且异常总是相同的,无论什么类型的值,我分配或不赋值或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.
在下面的api的描述中,我们可以看到一些属性带有()像strike()作为doulble,一些不带有()像secType作为字符串;有人告诉我,这可能是问题。请给我任何与此COM投射问题相关的提示:
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:
推荐答案
你能发布一些实际上失败的代码吗?
Can you post some code that actually fails?
我想发生的是你试图一个COM对象到一个纯.NET类型,并且不工作,因为COM对象需要通过运行时调用包装器或手动映射显式地映射到.NET类型。
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.
一个建议是,跳过显式转换并使用.NET 4.0中的动态类型来访问COM对象的属性,然后将它映射到您需要的任何属性/对象。这样,您就可以准确地看到对象的哪个部分不想映射。
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对象问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!