Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
815B

  1. using System;
  2. using System.Reflection;
  3. using System.Reflection.Emit;
  4. namespace Svelto.Utilities
  5. {
  6. //https://stackoverflow.com/questions/321650/how-do-i-set-a-field-value-in-an-c-sharp-expression-tree/321686#321686
  7. public static class FastInvoke<T> where T : class
  8. {
  9. public static Action<T, object> MakeSetter(FieldInfo field)
  10. {
  11. DynamicMethod m = new DynamicMethod("setter", typeof(void), new Type[] {typeof(T), typeof(object)});
  12. ILGenerator cg = m.GetILGenerator();
  13. // arg0.<field> = arg1
  14. cg.Emit(OpCodes.Ldarg_0);
  15. cg.Emit(OpCodes.Ldarg_1);
  16. cg.Emit(OpCodes.Stfld, field);
  17. cg.Emit(OpCodes.Ret);
  18. return (Action<T, object>) m.CreateDelegate(typeof(Action<T, object>));;
  19. }
  20. }
  21. }