我有一種類型是這樣的:如何將對象值轉換爲Type值?
public class TypeValue
{
public Type Type { get; private set; }
public object Value { get; private set; }
}
,所以我可以做這樣的事情:
TypeValue tv = ...
int count = (tv.Type) tv.Value;
但編譯器給我這個錯誤:
The type or namespace name 'tv' could not be found (are you missing a using directive or an assembly reference?)
如何實現這個?
謝謝,但爲什麼呢?類型不是關鍵字,對吧? – 2011-02-11 23:34:14
在你的例子中,你知道'count'是一個int,因爲你剛剛聲明瞭它。 – 2011-02-11 23:41:22
同意這一點。我必須考慮這一點,但我可能不需要這種演員。雖然我仍然感到驚訝,但我無法做到上述事情。例如,我怎麼能存儲「int」,所以它可以放在像上面的代碼?我想這是無法完成的。因爲如果「(type)value」正在使用實際的System.Type值,那麼它仍然會工作,因爲它會在編譯時知道,不是嗎? – 2011-02-11 23:52:48