I have big arrays of
KeyValuePair<DateTime,decimal>
GCHandle.Alloc
I just tested that unsafe
and GCHandle.Alloc
don't work (as you suggested). There is a terribly unsafe hack to still do this. I don't know if this is safe with the current CLR. It certainly is not guaranteed to work in the future.
You can convert an object reference of any type to any other reference type in IL. That IL will not be verifiable. The JIT tends to accept quite a few non-verifiable constructs. Maybe this is because they wanted to support Managed C++.
So you need to generate a DynamicMethod that roughly has the following IL:
static T UnsafeCast(object value) {
ldarg.1 //load type object
ret //return type T
}
I think this should work...
Or, you can call System.Runtime.CompilerServices.JitHelpers.UnsafeCast<T>
using Reflection.
This is a dangerous tool... I would not use it in production code.