I’m working on a rather tricky problem. At the very high level, I have an object which is a primitive and I need to coerce it into a different primitive type in C#. The values and the types are determined at runtime.
I tried roughly this (my code is more complex, this demonstrates the problem):
object value = (int)0x8ba9dc90;
Type t = typeof(UInt64);
object result = Convert.ChangeType(value, t);
This works some of the time, except in the case (as above) where an overflow or underflow would occur.
What I want to happen is that coercion (instead of conversion) would occur. In this case, I’d simply like that “(int)0x8ba9dc90” to be “(ulong)0x8ba9dc90”. Similar with floats: if value = “(float)-32.01” and “t” is “UInt64”, I want the result to be “0xffffffffffffffe0”. Which is exactly what you get when you run “unchecked { ulong u = (ulong)(double)-32.01; }”
Is there any way to do this, or am I stuck writing a custom converter?
(Yes, I realize this is an odd thing to try to do. This is all highly dynamic code, and I’m trying to do coercion in a DynamicObject.TryConvert override. I also am fully aware there are a lot of cases this will throw away data through down casts, etc. That’s perfectly fine in my application. I just can’t figure out how to write this without a giant nested switch statement.)
EDIT: To be clear my function looks something like this:
public override bool TryConvert(ConvertBinder binder, out object result)
{
if (binder.Type.IsPrimitive && m_type.IsPrimitive)
{
// m_type is System.Type, which is m_value's current type.
// m_value is System.Object, contains a primitive value.
// binder.Type is the System.Type I need to coerce m_value into.
result = Convert.ChangeType(m_value, binder.Type);
return true;
}
result = null;
return false;
}
You can use LINQ expressions to do this conversion. This article explains how.
The basic idea is to construct a LINQ expression equivalent to a cast expression.
Now you can call
convand pass it anobjectwrapping a value of the original type; it will return a coerced value back to you. This code will pick up your custom type conversions too (for your own types, not for primitives). If necessary, you can add more conversions in between by increasing the level of nesting of yourExpression.Convertobjects.