/// <summary> /// Adds a single action into the manager, automatically connecting /// accelerators and groups. /// </summary> /// <param name="newAction">The action to add.</param> public void Add(Action newAction) { // Make sure we have sane data. if (newAction == null) { throw new ArgumentNullException("action"); } // Check to see if we are going to filter out this action. if (!CanAdd(newAction)) { return; } // Associate the name of the action in the current dictionary. If the // name is already registered, then add a message and ignore the // request. string actionName = newAction.Name; if (actions.ContainsKey(actionName)) { // Create a new error message. var message = new SeverityMessage( Severity.Error, "Cannot register " + actionName + " action because it was already registered previously."); messages.Add(message); return; } actions[actionName] = newAction; // Figure out what action group this action is associated with. string groupName = "Global"; if (newAction is IConfigurableAction) { groupName = ((IConfigurableAction)newAction).GroupName; } ActionSet group = GetOrCreateGroup(groupName); group.Add(newAction); }
/// <summary> /// Scans the specified assembly and looks for all constructible actions /// with zero parameter constructors. For every one, it creates a new /// instance and adds it to the manager. /// </summary> /// <param name="assembly">The assembly.</param> public void Add(Assembly assembly) { // Make sure we don't have a null since we can't handle that. if (assembly == null) { throw new ArgumentNullException("assembly"); } // Scan through the types of the assembly. var emptyTypes = new Type[] { }; var emptyObjects = new object[] { }; Type actionType = typeof(Action); foreach (Type type in assembly.GetTypes()) { // Make sure the type is a concrete instance. if (!type.IsClass || type.IsAbstract) { // We can't create this type. continue; } // If we aren't an Action type, then just continue. if (!actionType.IsAssignableFrom(type)) { continue; } // Make sure we can add it. if (!CanAdd(type)) { continue; } // Determine if we have a parameterless constructor. ConstructorInfo constructor = type.GetConstructor(emptyTypes); if (constructor == null) { // Add a message to report this. var message = new SeverityMessage( Severity.Alert, "Cannot create an instance of " + type + " because it does not have a parameterless constructor."); messages.Add(message); // We are done with this type. continue; } // Create the item and add it to the manager. var action = (Action)constructor.Invoke(emptyObjects); Add(action); } }