2013-10-01 265 views
0

我正在使用MATLAB dll。它需要一個矩陣作爲int類型[,]作爲輸入,並輸出作爲類型:對象{雙[,]}在C#中將對象{double [,]}轉換爲double [,]或int [,] .net

private void pic_edges(int[,] array) 
    { 

     Class1 obj = new Class1(); 
     object res = null; 
     res = obj.edge_dll(1, array, .5); 
    } 

名稱;價值;型號

res; {object [1]}; object {object []}
[0]; {double [450,600]}; object {double [,]}

現在我想將對象{double [,]}更改爲int [,]或double [,]。但如何?

int[,] pic=null; 
double[,] pic2=nu1l; 

編輯:

我用下面的代碼:(感謝 '他現在誰不能被命名爲')

var objectArray = obj.edge_dll(1, array, .5); 
    double[,] pic3 = (double[,]) objectArray[0]; 

並正確轉換。
現在怎麼雙轉換[,]爲int [,]

我用這個代碼:(但有沒有更好的辦法?)

int[,] pic4 =new int[pic3.GetLength(0),pic3.GetLength(1)]; 
     for (var i = 0; i < pic3.GetLength(0); i++) 
      for (var j = 0; j < pic3.GetLength(1); j++) 
       pic4[i, j] = (int)pic3[i, j]; 

回答

1

你應該type-cast它。

如果我正確理解你的問題,你有東西從一個對象轉換爲整數數組。

嘗試這樣:

 var objectArray = obj.edge_dll(1, array, .5); 

     for (var index = 0; index <= objectArray.Count(); index++) 
     { 
      anIntegerArray[index] = (int) objectArray[index]; 
     } 
+0

@majidmobini:答案更新! –

+0

它說:不能取消'objectArray [0]'作爲'int' –

+0

這意味着objectArray [0]不是一個整數。你能告訴我們方法obj.edge_dll()的簽名嗎?它返回什麼? [如果它返回一個對象數組,上面應該已經工作了。]所以,你錯過了一些東西 –