2011-04-11 58 views
1

如何獲取對象並將其轉換爲結構體和副簽證?對象 - >結構?

public void myMethod1(object myInputObject, out string myOutputString) 
{ 
    myInputObject = null; 
    myOutputString = ""; 

    //Convert object into a struct, then do something 
} 
+8

你所說的「對象轉換成結構」是什麼意思?什麼對象?如果實際上提供了盒裝值,則可以取消裝箱,但不能僅從任何舊的參考類型創建結構。真的不清楚你想要做什麼。 – 2011-04-11 07:23:40

+2

談論拳擊/ unboxig? http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx – 2011-04-11 07:28:52

回答

0

我想你想是這樣的:(僞代碼)

class myclass 
{ 
public int a; 
public float b; 

} 

struct somestruct 
{ 

somestruct(int a, float b) 
{ 
    this.a = a; 
    this.b = b; 
} 

int a; 
float b; 

} 


myclass mc = new myclass(); 

mc.a = 125; 
mc.b = 12.5; 

somestruct s = new somestruct(mc.a, mc.b); //all fields of mc are now in struct somestruct 
0

在你的方法中定義一個struct和set字段值。