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.

30 lines
1.1KB

  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. if (field.FieldType.IsInterface == true && field.FieldType.IsValueType == false)
  12. {
  13. DynamicMethod m = new DynamicMethod("setter", typeof(void), new Type[] { typeof(T), typeof(object) });
  14. ILGenerator cg = m.GetILGenerator();
  15. // arg0.<field> = arg1
  16. cg.Emit(OpCodes.Ldarg_0);
  17. cg.Emit(OpCodes.Ldarg_1);
  18. cg.Emit(OpCodes.Stfld, field);
  19. cg.Emit(OpCodes.Ret);
  20. return (Action<T, object>)m.CreateDelegate(typeof(Action<T, object>));
  21. }
  22. throw new ArgumentException("<color=orange>Svelto.ECS</color> unsupported EntityView field (must be an interface and a class)");
  23. }
  24. }
  25. }