Esempio n. 1
0
        // --------------------------------------------------------------------------------------------
        /// <summary>
        /// Retrieves the CommandTargetInfo belonging to the target type.
        /// </summary>
        // --------------------------------------------------------------------------------------------
        private CommandTargetInfo GetTargetInfo()
        {
            CommandTargetInfo targetInfo;

            if (!_Targets.TryGetValue(EventTarget.GetType(), out targetInfo))
            {
                throw new InvalidOperationException(
                          (String.Format(Resources.CommandDispatcher_NoType, EventTarget.GetType())));
            }
            return(targetInfo);
        }
Esempio n. 2
0
        // --------------------------------------------------------------------------------------------
        /// <summary>
        /// Scans through all the methods of the EventTarget's type to collect information about
        /// command handler methods.
        /// </summary>
        // --------------------------------------------------------------------------------------------
        private void ScanDispatchInfo()
        {
            Type targetType = EventTarget.GetType();

            lock (_Targets)
            {
                // --- We scan only the type info, if we have not scanned it yet.
                if (_Targets.ContainsKey(targetType))
                {
                    return;
                }

                var target = new CommandTargetInfo();

                // --- Obtain the default command Guid for the event target
                var  defaultGuidAttr = targetType.GetAttribute <DefaultCommandGroupAttribute>();
                Guid defaultGuid     = defaultGuidAttr == null
          ? (GuidProvider == null ? targetType.GUID : GuidProvider.CommandGuid)
          : defaultGuidAttr.Value.GUID;
                target.DefaultGuid = defaultGuid;

                // --- Obtain command mapping information
                var mappings = targetType.AttributesOfType <CommandMapAttribute>();
                foreach (var attr in mappings)
                {
                    target.Mappings.Add(attr);
                }

                // --- Obtain all methods that can be used as command methods
                var commandMethods =
                    from method in targetType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic |
                                                         BindingFlags.Instance | BindingFlags.Static)
                    where method.ReturnType == typeof(void) &&
                    (
                        // --- void MyCommandExec()
                        method.GetParameters().Count() == 0 &&
                        Attribute.IsDefined(method, typeof(CommandExecMethodAttribute))
                        ||
                        // --- void MyCommandExec(CommandContext)
                        method.GetParameters().Count() == 1 &&
                        (
                            method.GetParameters()[0].ParameterType == typeof(CommandContext) ||
                            method.GetParameters()[0].ParameterType.IsSubclassOf(typeof(CommandContext))
                        ) &&
                        Attribute.IsDefined(method, typeof(CommandExecMethodAttribute))
                        ||
                        // --- void MyCommandMethod(OleMenuCommand)
                        method.GetParameters().Count() == 1 &&
                        method.GetParameters()[0].ParameterType == typeof(OleMenuCommand) &&
                        Attribute.IsDefined(method, typeof(CommandMethodAttribute))
                        ||
                        // --- void MyCommandMethod(OleMenuCommand, CommandExec)
                        method.GetParameters().Count() == 2 &&
                        method.GetParameters()[0].ParameterType == typeof(OleMenuCommand) &&
                        (
                            method.GetParameters()[1].ParameterType == typeof(CommandContext) ||
                            method.GetParameters()[1].ParameterType.IsSubclassOf(typeof(CommandContext))
                        ) &&
                        Attribute.IsDefined(method, typeof(CommandMethodAttribute))
                    ) &&
                    Attribute.IsDefined(method, typeof(CommandIdAttribute))
                    select method;
                foreach (var method in commandMethods)
                {
                    foreach (CommandIdAttribute idAttr in
                             method.GetCustomAttributes(typeof(CommandIdAttribute), false))
                    {
                        var menuInfo = ObtainCommandMethodAttributes(idAttr, method, defaultGuid);
                        MergeCommandMethodInfo(target, menuInfo);
                    }
                }
                _Targets.Add(targetType, target);
            }
        }