From: jpbruyere Date: Thu, 15 Dec 2016 10:57:05 +0000 (+0100) Subject: simplify datasourcechange dynMeth with a helper func in the Instantiator X-Git-Tag: v0.5.1~63^2~30 X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=aba894f6a5bb41200e969ccdd7045800ff36e74a;p=jp%2Fcrow.git simplify datasourcechange dynMeth with a helper func in the Instantiator --- diff --git a/src/Instantiator.cs b/src/Instantiator.cs index 6e66dd44..7db2967f 100644 --- a/src/Instantiator.cs +++ b/src/Instantiator.cs @@ -398,7 +398,6 @@ namespace Crow } NodeAddress currentNode = ctx.CurrentNodeAddress; - NodeAddress targetNA = getNodeAdressFromBindingExp (currentNode, bindingExp); string [] bindTrg = bindingExp.Last().Split ('.'); @@ -450,6 +449,9 @@ namespace Crow Debug.WriteLine ("Property less binding: " + Target + expression); #endif } + /// + /// Gets the node adress from binding expression splitted with '/' starting at a given node + /// NodeAddress getNodeAdressFromBindingExp(NodeAddress currentNode, string[] bindingExp){ int ptr = currentNode.Count - 1; @@ -463,7 +465,6 @@ namespace Crow break; ptr--; } - } else if (bindingExp [0] == "..") { //search starting at current node int levelUp = bindingExp.Length - 1; if (levelUp > ptr) @@ -511,9 +512,6 @@ namespace Crow PropertyInfo piSource = ctx.CurrentNodeType.GetProperty(sourceMember); Type sourceValueType = piSource.PropertyType; - //il.Emit (OpCodes.Call, typeof(object).GetMethod("GetType")); - //il.Emit (OpCodes.Call, typeof(Type).GetProperty("IsValueType").GetGetMethod()); - if (sourceValueType == typeof (string)) { il.Emit (OpCodes.Callvirt, CompilerServices.miObjToString); } else if (!sourceValueType.IsValueType) @@ -549,28 +547,12 @@ namespace Crow il.Emit (OpCodes.Nop); - //load new datasource onto the stack for handler addition at the end - il.Emit (OpCodes.Ldarg_2); - il.Emit (OpCodes.Ldfld, CompilerServices.fiDSCNewDS); - - //first we have to create delegate from cached dynMethod bound to the GraphicObject currently instanced - - //Load cached delegate il.Emit(OpCodes.Ldarg_0);//load ref to this instanciator onto the stack - il.Emit(OpCodes.Ldfld, typeof(Instantiator).GetField("dsValueChangedDynMeths", BindingFlags.Instance | BindingFlags.NonPublic)); - il.Emit(OpCodes.Ldc_I4, dmVC);//load index of dynmathod - il.Emit(OpCodes.Callvirt, typeof(List).GetMethod("get_Item", new Type[] { typeof(Int32) })); - - //load ds changed eventhandlertype - il.Emit(OpCodes.Ldtoken, typeof(EventHandler)); - il.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle", new - Type[1]{typeof(RuntimeTypeHandle)})); - il.Emit (OpCodes.Ldarg_1);//load datasource change source - il.Emit (OpCodes.Call, CompilerServices.miCreateBoundDelegate);//create bound delegate - - //add new delegate to datasource valuechanged event - il.Emit(OpCodes.Callvirt, typeof(IValueChange).GetEvent("ValueChanged").AddMethod);//call add event //il.Emit(OpCodes.Pop); + il.Emit (OpCodes.Ldarg_2);//load new datasource + il.Emit (OpCodes.Ldfld, CompilerServices.fiDSCNewDS); + il.Emit(OpCodes.Ldc_I4, dmVC);//load index of dynmathod + il.Emit (OpCodes.Call, typeof(Instantiator).GetMethod("dataSourceChangedEmitHelper", BindingFlags.Instance | BindingFlags.NonPublic)); il.Emit (OpCodes.Ret); //store dschange delegate in instatiator instance for access while instancing graphic object @@ -587,6 +569,10 @@ namespace Crow ctx.il.Emit(OpCodes.Callvirt, typeof(GraphicObject).GetEvent("DataSourceChanged").AddMethod);//call add event #endregion } + void dataSourceChangedEmitHelper(object dscSource, IValueChange dataSource, int dynMethIdx){ + dataSource.ValueChanged += + (EventHandler)dsValueChangedDynMeths [dynMethIdx].CreateDelegate (typeof(EventHandler), dscSource); + } /// /// Compile events expression in IML attributes, and store the result in the instanciator /// Those handlers will be bound when instatiing