我试图弄清楚当类型本身未知时支持将整数类型(short/int/long)拆箱到其固有类型的语法.
这是一个完全人为的例子,它展示了这个概念:
// Just a simple container that returns values as objects
struct DataStruct
{
public short ShortVale;
public int IntValue;
public long LongValue;
public object GetBoxedShortValue() { return ShortVale; }
public object GetBoxedIntValue() { return IntValue; }
public object GetBoxedLongValue() { return LongValue; }
}
static void Main( string[] args )
{
DataStruct data;
// Initialize data - any value will do
data.LongValue = data.IntValue = data.ShortVale = 42;
DataStruct newData;
// This works if you know the type …Run Code Online (Sandbox Code Playgroud)