I have a generic method for trying to get a generic value from a dictionary via key using my own TryGetValue
like (of course cropped a lot to the essential)
public class BaseExample
{
public virtual bool TryGetValue<T>(string key, out T value)
{
value = default;
return false;
}
}
public class Example : BaseExample
{
private Dictionary<string, Item> _items = new Dictionary<string, Item>();
public override bool TryGetValue<T>(string key, out T value)
{
value = default;
if (!GetItem(key, value, out var setting)) { return false; }
value = (T)setting.Value;
return true;
}
private bool GetItem<T>(string key, T value, out Item item)
{
item = null;
if (!_items.ContainsKey(key))
{
item = null;
return false;
}
item = _items[key];
return true;
}
}
This works in the Unity Editor but as soon as this is built to UWP and IL2CPP as soon as I try to run the method with e.g.
var value = example.TryGetValue<int>("SomeKey");
It throws a
System.ExecutionEngineException: Attempting to call method 'Example::TryGetValue<System.Int32>' for which no ahead of time (AOT) code was generated.
What could be the reason for this and how can I fix it?
Ok after some further research and tests the conclusion to why this happened is as follows:
Since according method was part of an interface and only either called via interface or via the base class the Example.TryGetValue
method was never explicitly called via this class.
Since the Example
class lives in a different Assembly than the interface and Base class the code for Example.TryGetValue
was somehow stripped of on compile time (since the compiler thinks it is never used anyway) and therefore missing later in the build.
I now know the reason and what to have in mind for the future but not yet a real solution except having an explicit implementation and calling that so it is not stripped of...