2011-12-30 63 views
0

我有幾個COM庫(TISCOPELib和MIPPARAMLib),我一直在使用C++(非託管),現在我轉換爲C#。COM互操作使用(從C++轉換爲C#/ .NET)

的這段代碼工作在C++:

TISCOPELib::IFilterBlockCasette *casette; 
... inialization ... 
int position = casette->Position; 
... other stuff ... 

在C#中,我會做下列之一:

TISCOPELib.IFilterBlockCasette casette = microscope.FilterBlockCasette1; // Init stuff. 
MIPPARAMLib.IMipParameter param = casette.Position; 
int position = param.RawValue; 
... other stuff ... 

或者

TISCOPELib.IFilterBlockCasette casette = microscope.FilterBlockCasette1; // Init stuff. 
int position = casette._Position; 
... other stuff ... 

如果我這樣做這個:

TISCOPELib.IFilterBlockCasette casette = microscope.FilterBlockCasette1; // Init stuff. 
int position = casette.Position; 
... other stuff ... 

我會得到以下錯誤:

An unhandled exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in Anonymously Hosted DynamicMethods Assembly

Additional information: Cannot implicitly convert type 'System.__ComObject' to 'int'

在這種情況下,System.__ComObject應該是IMipParameterintRawValue財產。

那麼這裏最好的行動是什麼?使用IMipParameter中間步驟,使用_Position,還是有另一種解決方案?如果我使用IMipParameter,有沒有辦法可以進行靜態類型檢查?

+0

COM支持默認屬性,與DISPID屬性0貌似RawValue是這樣的屬性。 C#不喜歡超出索引器的默認屬性,因爲屬性getter沒有參數,所以RawValue沒有資格。你最好把它拼出來。 – 2011-12-30 16:24:44

+0

我使用RawValue的主要問題是它有一個動態類型,所以我不能使用靜態類型檢查_Property。 – CookieOfFortune 2011-12-30 16:35:57

+0

我不明白這個問題,你不喜歡打一個下劃線或什麼的?你可以編輯互操作程序集的IL,但這有點嚴重。對象模型不是很清楚順便說一句。 – 2011-12-30 16:42:49

回答

1

嘗試

int position = (int)casette.Position.RawValue; 
+0

是的,這也適用,但我仍然沒有得到與_Position一樣的靜態類型檢查。 – CookieOfFortune 2011-12-30 16:10:04