I'm trying to figure out syntax that supports unboxing an integral type (short/int/long) to its intrinsic type, when the type itself is unknown.
Here is a completely contrived example that demonstrates the concept:
// 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 you are expecting!
newData.ShortVale = (short)data.GetBoxedShortValue();
newData.IntValue = (int)data.GetBoxedIntValue();
newData.LongValue = (long)data.GetBoxedLongValue();
// But what about when you don't know?
newData.ShortVale = data.GetBoxedShortValue(); // error
newData.IntValue = data.GetBoxedIntValue(); // error
newData.LongValue = data.GetBoxedLongValue(); // error
}
In each case, the integral types are consistent, so there should be some form of syntax that says "the object contains a simple type of X, return that as X (even though I don't know what X is)". Because the objects ultimately come from the same source, there really can't be a mismatch (short != long).
I apologize for the contrived example, it seemed like the best way to demonstrate the syntax.
Thanks.
I'm not completely sure about what you'd like to achieve with this, but your DataStruct type is errornous.
I suppose, not all of its methods return LongValue.
Otherwise, you can always use the Convert class to try to convert between different types.
For example:
Please clarify your post (just hit the edit button and edit it) if you meant something different.
By the way, converting from
object
can be quite error-prone as it is the base type of everything. So, anobject
can be anything, and that means that you can't always safely convert anobject
to an int or any other type.More examples:
And this is also useful:
I hope this helps.
You can return
dynamic
, which can then be cast to an integral type.Well, the
object
in itself is the most generic type the framework knows. Whether is is a boxed value type (including primitive) or something else doesn't matter; if you want to get more specific you have to do a typecast unless you remain in the "loosely typed" world withobject
(or, in C# 4,dynamic
).Note, however, that you can use a list of conditions to achieve what you want:
Edit: A generic "box" may also do what you want:
This can then be used instead of
object
; it is still an object reference but it is also strongly typed to the original structure or primitive type.As stated by others, your example won't work, because you return LongValue from each method, so you'll get an invalid cast exception here (a boxed long cannot be cast to short).
However, using C# 4's
dynamic
, this will work (note the fixes to your GetBoxed methods anddynamic
rather thanobject
:Note that you don't need any casts in the last three cases. Note also, however, that if the types don't align, like in
GetBoxedShortValue() { return LongValue; }
, the last three lines will result in invalid cast exceptions. (Interestingly the first three won't, they'll just work, but when you changedynamic
back intoobject
, they will throw invalid cast exceptions.)