I have the following function:
public static T TryGetArrayValue<T>(object[] array_, int index_)
{
... //some checking goes up here not relevant to question
dynamic boxed = array_[index_];
return (T)boxed;
}
When I call it in the following way,
object a = new object();
object v = TUtils.TryGetArrayValue<object>(new object[] { a }, 0);
(T)boxed
throws a null reference exception.
Any other type I put in there other than "object", it works perfectly fine.
Any ideas what this is, and why it's throwing the exception?
Edit:
The reason why I use dynamic is to avoid exception when converting types, for example:
double a = 123;
int v = TUtils.TryGetArrayValue<int>(new object[] { a }, 0);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…