public static IEnumerable <CodeInstruction> Transpiler(MethodBase original, IEnumerable <CodeInstruction> instructions) { Validate.NotNull(INJECTION_OPERAND); foreach (CodeInstruction instruction in instructions) { if (instruction.opcode.Equals(INJECTION_OPCODE) && instruction.operand.Equals(INJECTION_OPERAND)) { /* * int prev = GameInput.GetMaximumEnumValue(typeof(GameInput.Button)) + 1; * // ^ This value is already calculated by the original code, it's stored on top of the stack. * KeyBindingManager keyBindingManager = new KeyBindingManager(); * GameButton.numButtons = Math.Max(keyBindingManager.GetHighestKeyBindingValue() + 1, prev); */ yield return(new CodeInstruction(OpCodes.Newobj, Reflect.Constructor(() => new KeyBindingManager()))); yield return(new CodeInstruction(OpCodes.Callvirt, Reflect.Method((KeyBindingManager t) => t.GetHighestKeyBindingValue()))); yield return(new CodeInstruction(OpCodes.Ldc_I4_1)); yield return(new CodeInstruction(OpCodes.Add)); yield return(new CodeInstruction(OpCodes.Call, Reflect.Method(() => Math.Max(default(int), default(int))))); } yield return(instruction); } }
public static void Main() { ConstructorInvoker ctor = Reflect.Constructor(typeof(long)); MemberGetter getValue = Reflect.Getter(typeof(long), "m_value"); MemberSetter setValue = Reflect.Setter(typeof(long), "m_value"); long integer = (long)ctor(); ValueTypeHolder holder = new ValueTypeHolder(integer); // IMPORTANT! setValue(holder, 8L); Console.WriteLine(getValue(holder)); Console.ReadLine(); }
public static void Main() { ConstructorInvoker ctor = Reflect.Constructor(typeof(Person), typeof(string), typeof(int)); MemberGetter getName = Reflect.Getter(typeof(Person), "Name"); MemberGetter getAge = Reflect.FieldGetter(typeof(Person), "Age"); MemberSetter setAge = Reflect.Setter(typeof(Person), "Age"); MultiSetter setBoth = Reflect.MultiSetter(typeof(Person), "Age", "Name"); Person person = (Person)ctor("John Doe", 21); setAge(person, 30); Console.WriteLine(getName(person)); Console.WriteLine(getAge(person)); setBoth(person, 35, "John Wick"); Console.WriteLine(getName(person)); Console.WriteLine(getAge(person)); Console.ReadLine(); }
public static void Main() { ConstructorInvoker ctor = Reflect.Constructor(typeof(Animal), typeof(string), typeof(int)); MemberGetter getName = Reflect.Getter(typeof(Animal), "Name"); MemberGetter getAge = Reflect.FieldGetter(typeof(Animal), "Age"); MemberSetter setAge = Reflect.Setter(typeof(Animal), "Age"); MultiSetter setBoth = Reflect.MultiSetter(typeof(Animal), "Age", "Name"); Animal animal = (Animal)ctor("Charlie", 5); ValueTypeHolder holder = new ValueTypeHolder(animal); // IMPORTANT! setAge(holder, 8); Console.WriteLine(getName(holder)); Console.WriteLine(getAge(holder)); setBoth(holder, 10, "Buster"); Console.WriteLine(getName(holder)); Console.WriteLine(getAge(holder)); Console.ReadLine(); }
/// <summary> /// Creates a delegate which can create instance based on the constructor <paramref name="ctorInfo"/>. /// </summary> public static ConstructorInvoker DelegateForCreateInstance(this ConstructorInfo ctorInfo) { ConstructorInvoker ctor = Reflect.Constructor(ctorInfo); return(ctor); }
/// <summary> /// Creates a delegate which can invoke the constructor whose parameter types are <paramref name="parameterTypes" /> /// and matching <paramref name="bindingFlags"/> on the given <paramref name="type"/>. /// Leave <paramref name="parameterTypes"/> empty if the constructor has no argument. /// </summary> public static ConstructorInvoker DelegateForCreateInstance(this Type type, FasterflectFlags bindingFlags, params Type[] parameterTypes) { return(Reflect.Constructor(ReflectLookup.Constructor(type, bindingFlags, parameterTypes))); }
/// <summary> /// Creates a delegate which can invoke the constructor whose parameter types are <paramref name="parameterTypes" /> /// on the given <paramref name="type"/>. Leave <paramref name="parameterTypes"/> empty if the constructor /// has no argument. /// </summary> public static ConstructorInvoker DelegateForCreateInstance(this Type type, params Type[] parameterTypes) { return(Reflect.Constructor(type, parameterTypes)); }
/// <summary> /// Invokes a constructor whose parameter types are <paramref name="parameterTypes" /> and /// matching <paramref name="bindingFlags"/> on the given <paramref name="type"/> /// with <paramref name="parameters" /> being the arguments. /// </summary> public static object CreateInstance(this Type type, Type[] parameterTypes, FasterflectFlags bindingFlags, params object[] parameters) { return(Reflect.Constructor(type, bindingFlags, parameterTypes)(parameters)); }
/// <summary> /// Invokes a constructor whose parameter types are inferred from <paramref name="parameters" /> /// on the given <paramref name="type"/> with <paramref name="parameters" /> being the arguments. /// Leave <paramref name="parameters"/> empty if the constructor has no argument. /// </summary> /// <remarks> /// All elements of <paramref name="parameters"/> must not be <see langword="null"/>. Otherwise, /// <see cref="NullReferenceException"/> is thrown. If you are not sure as to whether /// any element is <see langword="null"/> or not, use the overload that accepts params <see cref="Type"/> array. /// </remarks> /// <seealso cref="CreateInstance(Type, Type[], object[])"/> public static object CreateInstance(this Type type, params object[] parameters) { return(Reflect.Constructor(type, parameters.ToTypeArray())(parameters)); }
public void Constructor() { ConstructorInfo method = Reflect.Constructor(() => new KeyBindingManager()); method.DeclaringType.Should().Be <KeyBindingManager>(); }