/// <summary> /// Creates a new ParserControllerDefinition instance based on the Type and fills the actions and outlets /// declated on the type /// </summary> /// <param name="name">The name of the controller type</param> /// <param name="type">The type of the controller</param> /// <returns>The populated definition</returns> private static ParserControllerDefinition LoadControllerDefinition(string name, Type type) { ParserControllerDefinition defn = new ParserControllerDefinition(name, type); FillControllerOutlets(defn); FillControllerActions(defn); return(defn); }
/// <summary> /// Fills the definitions outlets (properties and fields) with all the declared outlets on the type /// </summary> /// <param name="defn"></param> private static void FillControllerOutlets(ParserControllerDefinition defn) { PropertyInfo[] allprops = defn.ControllerType.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo aprop in allprops) { //Don't use the standard methods as the bug in the .Net framework for inherited PropertyInfo.GetCustomAttributes //Will not return properties where they are set to be inherited. //Use the System.Attribute.GetCustomAttribute method instead. //Keeping the same method for main factory parsing as that is tested and in-use. Attribute found = System.Attribute.GetCustomAttribute(aprop, typeof(PDFOutletAttribute), true); if (found != null) { PDFOutletAttribute outletAttr = (PDFOutletAttribute)found; if (outletAttr.IsOutlet) { ParserControllerOutlet outlet = new ParserControllerOutlet(aprop, outletAttr.ComponentID, outletAttr.Required); if (defn.Actions.Contains(outlet.ID)) { throw new PDFParserException(string.Format(Errors.ControllerAlreadyHasOutletWithID, defn.ControllerTypeName, outlet.ID)); } defn.Outlets.Add(outlet); } } } FieldInfo[] allfileds = defn.ControllerType.GetFields(BindingFlags.Public | BindingFlags.Instance); if (null != allfileds) { foreach (FieldInfo afield in allfileds) { Attribute found = System.Attribute.GetCustomAttribute(afield, typeof(PDFOutletAttribute), true); if (found != null) { PDFOutletAttribute outletAttr = (PDFOutletAttribute)found; if (outletAttr.IsOutlet) { ParserControllerOutlet outlet = new ParserControllerOutlet(afield, outletAttr.ComponentID, outletAttr.Required); if (defn.Actions.Contains(outlet.ID)) { throw new PDFParserException(string.Format(Errors.ControllerAlreadyHasOutletWithID, defn.ControllerTypeName, outlet.ID)); } defn.Outlets.Add(outlet); } } } } }
/// <summary> /// Fills the definitions actions (methods) with all the declared actions on its type /// </summary> /// <param name="defn"></param> private static void FillControllerActions(ParserControllerDefinition defn) { MethodInfo[] allmeths = defn.ControllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance); foreach (MethodInfo ameth in allmeths) { Attribute found = System.Attribute.GetCustomAttribute(ameth, typeof(PDFActionAttribute), true); if (found != null) { PDFActionAttribute actionAttr = (PDFActionAttribute)found; if (actionAttr.IsAction) { ParserControllerAction action = new ParserControllerAction(ameth, actionAttr.Name); if (defn.Actions.Contains(action.Name)) { throw new PDFParserException(string.Format(Errors.ControllerAlreadyHasActionName, defn.ControllerTypeName, action.Name)); } defn.Actions.Add(action); } } } }