public void Performance_test() { var rules = new XyzRules(); // warm up { var instance = new Xyz(); var xyz = new InterfaceWrapper <Xyz>(instance, rules); xyz.Target.X = 1; } var sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 1000; i++) { var instance = new Xyz(); var xyz = new InterfaceWrapper <Xyz>(instance, rules); xyz.Target.X = 1; } sw.Stop(); Console.WriteLine($"took {sw.ElapsedMilliseconds} ms"); }
private void CacheImplementations(SerializedProperty property) { if (!cached) { var propertyType = fieldInfo.FieldType; var interfaceType = propertyType.BaseType.GetGenericArguments()[0]; implementations = propertyType.Assembly.GetTypes() .Where(type => type.GetInterfaces().Contains(interfaceType)).ToList(); implementations.Insert(0, interfaceType); implementationsNames = implementations.Select(type => type.IsInterface ? "<none>" : type.Name).ToArray(); wrapper = (InterfaceWrapper)GetValue(property); try { wrapper.TypeID = implementations.FindIndex(type => type == wrapper.Type); } catch (Exception) { Debug.LogWarning("Type is missing!", property.serializedObject.targetObject); wrapper.TypeID = 0; } cached = true; } }
public void Intercept_changes_with_interface_wrapper_without_interface() { { var instance = new Xyz(); var xyz = new InterfaceWrapper <Xyz>(instance, new XyzRules()); xyz.Target.X = 1; Assert.AreEqual(2, instance.Y); Assert.AreEqual(4, instance.Z); } }
public void Counterparty_change_fills_product2() { var trade = new CdsTrade2 { Product = new CreditDefaultSwap2() }; var p = new InterfaceWrapper <CdsTrade2>(trade, new CdsRules2()).Target; p.CdsProduct.RefEntity = "AXA"; p.Counterparty = "CHASEOTC"; Assert.AreEqual("ICEURO", trade.ClearingHouse); Assert.AreEqual("MMR", trade.CdsProduct.Restructuring); Assert.AreEqual("SNR", trade.CdsProduct.Seniority); }
public void Intercept_changes_with_interface_wrapper() { { var instance = new Abcd(); var rules = new AbcdRules(); Assert.AreEqual(4, rules.RulesCount); var abcd = new InterfaceWrapper <IAbcd>(instance, rules); var inotify = (INotifyPropertyChanged)abcd; var changed = new List <string>(); inotify.PropertyChanged += (sender, args) => changed.Add(args.PropertyName); abcd.Target.A = 1; Assert.AreEqual(100, abcd.Target.A); Assert.AreEqual(100, instance.A); Assert.AreEqual(4, changed.Count); } { var instance = new Bingo(); var bingo = new InterfaceWrapper <IBingo>(instance, new BingoRules()); var inotify = (INotifyPropertyChanged)bingo; var changed = new List <string>(); inotify.PropertyChanged += (sender, args) => changed.Add(args.PropertyName); bingo.Target.X = 1; Assert.AreEqual("BINGO", bingo.Target.Message); Assert.AreEqual(101, instance.X); Assert.AreEqual(3, changed.Count); bingo.Target.Message = "BONGO"; Assert.AreEqual("BONGO", bingo.Target.Message); } }
private static object Create(Type targetType, object instance, Action <MethodInfo, Type> onError = null) { if (targetType == null) { throw new ArgumentNullException("targetType"); } if (targetType.IsPublic == false) { throw new ArgumentOutOfRangeException("targetType", targetType.FullName, "TargetType is not public."); } if ((targetType.IsInterface || targetType.IsAbstract) == false) { throw new ArgumentOutOfRangeException("targetType", targetType.FullName, "TargetType is not an interface or abstract type."); } if (instance == null) { throw new ArgumentNullException("instance"); } Type instanceType = instance.GetType(); if (instanceType.IsPublic == false) { throw new ArgumentOutOfRangeException("instance", instanceType.FullName, "Instance not of a public type."); } if (targetType.IsAssignableFrom(instanceType)) { return(instance); } Tuple <Type, Type> key = new Tuple <Type, Type>(targetType, instanceType); Type wrapperType; lock (_wrappers) { if (_wrappers.TryGetValue(key, out wrapperType) == false) { _wrappers.Add(key, wrapperType = InterfaceWrapper.GenerateWrapper(key.Item1, key.Item2, onError)); } } var result = Activator.CreateInstance(wrapperType); var ifwb = result as IInterfaceWrapper; return(ifwb.SetWrapped(instance)); }
public static Func <bool> EmitCondition(ref IDebuggable i_spectrum, BreakpointInfo i_breakpointInfo) { Func <bool> o_ILOut = null; if (i_breakpointInfo.AccessType == BreakPointConditionType.registryVsValue) { //e.g. PC == #0038 Type[] args = { typeof(CpuRegs) }; DynamicMethod dynamicMethod = new DynamicMethod( "RegVsValue", typeof(bool), //return type args, //arguments for the method typeof(CpuRegs).Module); //module as input ILGenerator il = dynamicMethod.GetILGenerator(); //1.Arg0 - registry il.Emit(OpCodes.Ldarg_0); // load m_spectrum.CPU.regs on stack FieldInfo testInfo1 = typeof(CpuRegs).GetField(i_breakpointInfo.LeftCondition, BindingFlags.Public | BindingFlags.Instance); il.Emit(OpCodes.Ldfld, testInfo1); //2.Arg1 - number il.Emit(OpCodes.Ldc_I4, (int)i_breakpointInfo.RightValue); //3.Compare EmitComparison(il, i_breakpointInfo.ConditionTypeSign); il.Emit(OpCodes.Ret); //Return: 1 => true(breakpoint is reached) otherwise 0 => false o_ILOut = (Func <bool>)dynamicMethod.CreateDelegate(typeof(Func <bool>), i_spectrum.CPU.regs); } else if (i_breakpointInfo.AccessType == BreakPointConditionType.flagVsValue) { //e.g.: fZ == 1 Type[] args = { typeof(CpuRegs) }; DynamicMethod dynamicMethod = new DynamicMethod( "FlagVsValue", typeof(bool), //return type args, //arguments for the method typeof(CpuRegs).Module); //module as input ILGenerator il = dynamicMethod.GetILGenerator(); //1.Arg0 - flag il.Emit(OpCodes.Ldarg_0); // load m_spectrum.CPU.regs on stack FieldInfo testInfo1 = typeof(CpuRegs).GetField("AF", BindingFlags.Public | BindingFlags.Instance); il.Emit(OpCodes.Ldfld, testInfo1); //get flag value(0 or 1) switch (i_breakpointInfo.LeftCondition) { case "FS": il.Emit(OpCodes.Ldc_I4, 0x80); break; case "FZ": il.Emit(OpCodes.Ldc_I4, 0x40); break; case "FH": il.Emit(OpCodes.Ldc_I4, 0x10); break; case "FPV": il.Emit(OpCodes.Ldc_I4, 0x04); break; case "FN": il.Emit(OpCodes.Ldc_I4, 0x02); break; case "FC": il.Emit(OpCodes.Ldc_I4, 0x01); break; default: throw new CommandParseException("Incorrect flag in condition emit..."); } il.Emit(OpCodes.And); //get flag value from F registry if (i_breakpointInfo.ConditionTypeSign == "==" && i_breakpointInfo.RightValue > 0) { i_breakpointInfo.RightValue = 0; i_breakpointInfo.ConditionTypeSign = "!="; } //2. Arg1 - right condition(must a number) il.Emit(OpCodes.Ldc_I4, (int)i_breakpointInfo.RightValue); //3. Compare EmitComparison(il, i_breakpointInfo.ConditionTypeSign); il.Emit(OpCodes.Ret); //Return: 1 => true(breakpoint is reached) otherwise 0 => false o_ILOut = (Func <bool>)dynamicMethod.CreateDelegate(typeof(Func <bool>), i_spectrum.CPU.regs); } else if (i_breakpointInfo.AccessType == BreakPointConditionType.memoryVsValue) { //e.g. (16384) == #FF00 //ToDo: Because it is not possible to dynamically emit code for interface method(IDebuggable.ReadMemory) // I temporary wrapped it into custom wrapper. InterfaceWrapper middleMan = new InterfaceWrapper(); middleMan.wrapInterface(i_spectrum); MethodInfo ReadMemoryMethod; if (i_breakpointInfo.RightValue > 0xFF) { ReadMemoryMethod = typeof(InterfaceWrapper).GetMethod("invokeReadMemory16Bit", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic , null , new Type[] { typeof(ushort) } , null); } else { ReadMemoryMethod = typeof(InterfaceWrapper).GetMethod("invokeReadMemory8Bit", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic , null , new Type[] { typeof(ushort) } , null); } DynamicMethod dynamicMethod = new DynamicMethod("ReadMemory" , typeof(bool) , new Type[] { typeof(InterfaceWrapper) } , typeof(InterfaceWrapper).Module ); ILGenerator IL = dynamicMethod.GetILGenerator(); //Arg0 - memory reference(static), e.g. (16384) IL.Emit(OpCodes.Ldarg_0); // load InterfaceWrapper on stack IL.Emit(OpCodes.Ldc_I4, i_breakpointInfo.LeftValue); // method parameter(for ReadMemoryMethod) IL.Emit(OpCodes.Call, ReadMemoryMethod); //Arg1 IL.Emit(OpCodes.Ldc_I4, i_breakpointInfo.RightValue); // <- compare to 8 or 16bit EmitComparison(IL, i_breakpointInfo.ConditionTypeSign); IL.Emit(OpCodes.Ret); //Return: 1 => true(breakpoint is reached) otherwise 0 => false o_ILOut = (Func <bool>)dynamicMethod.CreateDelegate(typeof(Func <bool>), middleMan); } else if (i_breakpointInfo.AccessType == BreakPointConditionType.registryMemoryReferenceVsValue) { // e.g.: (PC) == #D155 - instruction breakpoint //ToDo: Because it is not possible to dynamically emit code for interface method(IDebuggable.ReadMemory) // I temporary wrapped it into custom wrapper. InterfaceWrapper middleMan = new InterfaceWrapper(); middleMan.wrapInterface(i_spectrum); middleMan.wrapFields(i_spectrum.CPU.regs); MethodInfo ReadMemoryMethod; //Type[] args = { typeof(REGS) }; if (i_breakpointInfo.RightValue > 0xFF) { ReadMemoryMethod = typeof(InterfaceWrapper).GetMethod("invokeReadMemory16BitViaRegistryValue", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic , null , new Type[] { typeof(string) } , null); } else { ReadMemoryMethod = typeof(InterfaceWrapper).GetMethod("invokeReadMemory8BitViaRegistryValue", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic , null , new Type[] { typeof(string) } , null); } DynamicMethod dynamicMethod = new DynamicMethod("ReadMemoryViaRegistry", typeof(bool), new Type[] { typeof(InterfaceWrapper) }); ILGenerator IL = dynamicMethod.GetILGenerator(); //Arg0, e.g. (PC) IL.Emit(OpCodes.Ldarg_0); // load InterfaceWrapper on stack string registry = DebuggerManager.getRegistryFromReference(i_breakpointInfo.LeftCondition); IL.Emit(OpCodes.Ldstr, registry); IL.Emit(OpCodes.Call, ReadMemoryMethod); //Arg1, number(right condition) IL.Emit(OpCodes.Ldc_I4, i_breakpointInfo.RightValue); // <- compare to 8 or 16bit EmitComparison(IL, i_breakpointInfo.ConditionTypeSign); IL.Emit(OpCodes.Ret); //Return: 1 => true(breakpoint is reached) otherwise 0 => false o_ILOut = (Func <bool>)dynamicMethod.CreateDelegate(typeof(Func <bool>), middleMan); } return(o_ILOut); }