/// <summary> /// Scan an assembly to find all controls that can be created using XML. /// </summary> /// <param name="assembly">Assembly.</param> public static void ScanControls(Assembly assembly) { foreach (Type type in assembly.GetTypes().Where(t => t.IsDefined(typeof(XmlControlAttribute), false))) { object[] attribs = type.GetCustomAttributes(typeof(XmlControlAttribute), false); if (attribs.Length > 0) { XmlControlAttribute attrib = attribs[0] as XmlControlAttribute; if (attrib != null) { ElementHandler handler; if (attrib.CustomHandler != null) { MethodInfo mi = type.GetMethod(attrib.CustomHandler, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); if (mi != null) { handler = Delegate.CreateDelegate(typeof(ElementHandler), mi) as ElementHandler; } else { throw new Exception("Elemant handler not found."); } } else { handler = DefaultElementHandler; } string name = attrib.ElementName != null ? attrib.ElementName : type.Name; RegisterElement(name, type, handler); } } } }
/// <summary> /// Parse typed element and call it's handler. /// </summary> /// <typeparam name="T">Control type to be created.</typeparam> /// <param name="parent">Parent control.</param> /// <returns>Control.</returns> public T ParseElement <T>(Gwen.Control.ControlBase parent) where T : Gwen.Control.ControlBase { Type type = typeof(T); XmlControlAttribute attrib = null; object[] attribs = type.GetCustomAttributes(typeof(XmlControlAttribute), false); if (attribs.Length > 0) { attrib = attribs[0] as XmlControlAttribute; } ElementDef elementDef; if (m_ElementHandlers.TryGetValue(attrib != null && attrib.ElementName != null ? attrib.ElementName : type.Name, out elementDef)) { if (elementDef.Type == type) { m_CurrentElement = elementDef; return(elementDef.Handler(this, elementDef.Type, parent) as T); } } return(null); }