相关疑难解决方法(0)

拆箱到未知类型

我试图弄清楚当类型本身未知时支持将整数类型(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)

.net c# unboxing object inferred-type

6
推荐指数
1
解决办法
3599
查看次数

标签 统计

.net ×1

c# ×1

inferred-type ×1

object ×1

unboxing ×1