How to test whether a value is boxed in C# / .NET?

后端 未结 9 1320
终归单人心
终归单人心 2020-12-13 00:32

I\'m looking for a way to write code that tests whether a value is boxed.

My preliminary investigations indicate that .NET goes out of its way to conceal the fact,

9条回答
  •  醉酒成梦
    2020-12-13 01:20

    I think actually the question is kind of misstated. Isn't the question actually, "How can I tell if an object is a box for another type?"

    With reference to Allon's comment, if you have an object of type Object and the object is a primitive value type, it's a box. I'm not certain this is 100% correct, but (similar to Allon's implementation):

    // Assume there is some object o.
    bool isBoxed = o.GetType().IsPrimitive;
    

提交回复
热议问题