If I have an instance of object , and I know that it is actually a boxed integer, then I can simply return it back to int as follows:
object o = GetSomethingByName("foo"); int i = (int)o;
However, I really do not know that this is an integer. I only know that it can be assigned to an integer. For example, it could be a byte , and the above code will throw an InvalidCastException in this case. Instead, I would have to do this:
object o = GetSomethingByName("foo"); int i = (int)(byte)o;
The value can also be short or something else that can be assigned to int . How to generalize my code to handle all of these cases (without processing each feature separately)?
source share