1

我有一个方法可以接受许多不同类型的对象进行存储:

public void Store<T>(T item)
{
    // works fine
    if (item is Foo)
    {
      // ...
    }
    // works fine
    else if (item is Observation<ImageSignal>)
    {
      // ...
    }

    // isn't detected
    else if (item is Observation<Signal<ISpectrum>>)
    {
      // ...
    }

    else 
    {
      // Observation<Signal<ISpectrum>> always hits this.
      throw new NotSupportedException();
    }
}

谁能告诉我如何检测到这一点?

编辑: 我实际上是在传递一个包装这个对象的对象。埃里克是对的。问题解决了。不过,感谢您的快速回复。

4

2 回答 2

5

In this case wouldn't it be better to overload the Store function? It would be much easier to follow the logic.

public void Store(Foo item)
{
}

public void Store(Observation<ImageSignal> item)
{
}

public void Store(Observation<Signal<ISpectrum>> item)
{
}
于 2012-06-04T14:47:19.113 回答
0

typeof(T) or item.GetType(). hth

于 2012-06-04T14:45:28.080 回答