-
I'm trying to make my Reflection usage faster - and it works great in the Unity Editor - but, in the live game, I get the following error:
InvalidProgramException: Invalid IL code in DaggerfallWorkshop.Game.Mods.Ironman.ReflectionHelper:GetFieldGetter<TClass, TResult> (object,string,System.Type,System.Reflection.BindingFlags): method body is empty.
I'm pretty certain there's nothing wrong with the code, unless there's something special you need to do in DFUnity to make IL generation work from a mod. Code below:
Code: Select all
public static Func<TClass, TResult> GetFieldGetter<TClass, TResult>(this object obj, string field, Type startingType = null, BindingFlags bindingFlags = _BindingFlags)
{
Type type = typeof(TClass);
// GetFieldInfo is a custom function that returns a FieldInfo object.
FieldInfo info = GetFieldInfo(obj, field, startingType, bindingFlags);
DynamicMethod DM = new DynamicMethod("dynamic_get_" + info.Name, typeof(TResult), new Type[] { type }, type);
ILGenerator ILG = DM.GetILGenerator();
ILG.Emit(OpCodes.Ldarg_0);
ILG.Emit(OpCodes.Ldfld, info);
ILG.Emit(OpCodes.Ret);
return (Func<TClass, TResult>)DM.CreateDelegate(typeof(Func<TClass, TResult>));
}