Ourpalm / ILRuntime

Pure C# IL Intepreter Runtime, which is fast and reliable for scripting requirement on enviorments, where jitting isn't possible.
Other
2.99k stars 655 forks source link

Unable to cast object of type 'Int32' to type 'Dictionary`2'. #771

Open hui1993 opened 6 months ago

hui1993 commented 6 months ago

偶现的类型转换错误

代码如下: public bool TryGetItem(Hex hex, out T item) { var key = GetHexkey(hex); if(_map.ContainsKey(key)) { item = _map[key]; return true; } item = default; return false; }

_map是dictionary

可能跟范型有关,准备去掉试试

hui1993 commented 5 months ago

简化了代码,去掉范型约束之后,线上无报错。猜测跟范型约束有关,其他小伙伴可以参考

lixu19950414 commented 4 months ago

ILMethod中设置def.Body = null,结合泛型类型使用就会有小概率造成这个问题。具体原理不明。2.1.0版本,不开启jit

if !DEBUG || DISABLE_ILRUNTIME_DEBUG

            //Release Method body to save memory
            if(!noRelease)
                def.Body = null;

endif

注释掉def.Body = null 可以解决,但是会增加很多堆内存