public static List <GraphicObject> GetChildren(this GraphicObject go) { Type goType = go.GetType(); if (typeof(Group).IsAssignableFrom(goType)) { return((go as Group).Children); } if (typeof(Container).IsAssignableFrom(goType)) { return(new List <GraphicObject>(new GraphicObject[] { (go as Container).Child })); } if (typeof(TemplatedContainer).IsAssignableFrom(goType)) { return(new List <GraphicObject>(new GraphicObject[] { (go as TemplatedContainer).Content })); } if (typeof(TemplatedGroup).IsAssignableFrom(goType)) { return((go as TemplatedGroup).Items); } return(new List <GraphicObject>()); }
/// <summary> /// Compile events expression in GOML attributes /// </summary> /// <param name="binding">Event binding details</param> public static void CompileEventSource(Binding binding) { #if DEBUG_BINDING Debug.WriteLine("\tCompile Event Source => " + binding.ToString()); #endif Type target_type = binding.Source.Instance.GetType(); #region Retrieve EventHandler parameter type MethodInfo evtInvoke = binding.Source.Event.EventHandlerType.GetMethod("Invoke"); ParameterInfo [] evtParams = evtInvoke.GetParameters(); Type handlerArgsType = evtParams [1].ParameterType; #endregion Type [] args = { typeof(object), typeof(object), handlerArgsType }; DynamicMethod dm = new DynamicMethod(binding.CreateNewDynMethodId(), typeof(void), args, target_type); #region IL generation ILGenerator il = dm.GetILGenerator(256); string src = binding.Expression.Trim(); if (!(src.StartsWith("{") || src.EndsWith("}"))) { throw new Exception(string.Format("GOML:Malformed {0} Event handler: {1}", binding.Source.Member.Name, binding.Expression)); } src = src.Substring(1, src.Length - 2); string [] srcLines = src.Split(new char [] { ';' }); foreach (string srcLine in srcLines) { string statement = srcLine.Trim(); string [] operandes = statement.Split(new char [] { '=' }); if (operandes.Length < 2) //not an affectation { continue; } string lop = operandes [0].Trim(); string rop = operandes [operandes.Length - 1].Trim(); #region LEFT OPERANDES GraphicObject lopObj = binding.Source.Instance as GraphicObject; //default left operand base object is //the first arg (object sender) of the event handler il.Emit(OpCodes.Ldarg_0); //load sender ref onto the stack string [] lopParts = lop.Split(new char [] { '.' }); if (lopParts.Length > 1) //should search also for member of es.Source { MethodInfo FindByNameMi = typeof(GraphicObject).GetMethod("FindByName"); for (int j = 0; j < lopParts.Length - 1; j++) { il.Emit(OpCodes.Ldstr, lopParts [j]); il.Emit(OpCodes.Callvirt, FindByNameMi); } } int i = lopParts.Length - 1; MemberInfo [] lopMbis = lopObj.GetType().GetMember(lopParts [i]); if (lopMbis.Length < 1) { throw new Exception(string.Format("CROW BINDING: Member not found '{0}'", lop)); } OpCode lopSetOC; dynamic lopSetMbi; Type lopT = null; switch (lopMbis [0].MemberType) { case MemberTypes.Property: PropertyInfo lopPi = target_type.GetProperty(lopParts [i]); MethodInfo dstMi = lopPi.GetSetMethod(); lopT = lopPi.PropertyType; lopSetMbi = dstMi; lopSetOC = OpCodes.Callvirt; break; case MemberTypes.Field: FieldInfo dstFi = target_type.GetField(lopParts [i]); lopT = dstFi.FieldType; lopSetMbi = dstFi; lopSetOC = OpCodes.Stfld; break; default: throw new Exception(string.Format("GOML:member type not handle: {0}", lopParts [i])); } #endregion #region RIGHT OPERANDES if (rop.StartsWith("\'")) { if (!rop.EndsWith("\'")) { throw new Exception(string.Format ("GOML:malformed string constant in handler: {0}", rop)); } string strcst = rop.Substring(1, rop.Length - 2); il.Emit(OpCodes.Ldstr, strcst); } else { if (lopT.IsEnum) { throw new NotImplementedException(); } MethodInfo lopParseMi = lopT.GetMethod("Parse"); if (lopParseMi == null) { throw new Exception(string.Format ("GOML:no parse method found in: {0}", lopT.Name)); } il.Emit(OpCodes.Ldstr, rop); il.Emit(OpCodes.Callvirt, lopParseMi); il.Emit(OpCodes.Unbox_Any, lopT); } #endregion //emit left operand assignment il.Emit(lopSetOC, lopSetMbi); } il.Emit(OpCodes.Ret); #endregion Delegate del = dm.CreateDelegate(binding.Source.Event.EventHandlerType, binding.Source.Instance); MethodInfo addHandler = binding.Source.Event.GetAddMethod(); addHandler.Invoke(binding.Source.Instance, new object [] { del }); binding.Resolved = true; }
public static string GetIcon(this GraphicObject go) { return("#Crow.Coding.icons.toolbox." + go.GetType().FullName + ".svg"); }