Ejemplo n.º 1
0
        static MenuHelper()
        {
            /*
             * Create one instance of guiControllerCommand for each existing public command.
             * This instance is later used in every context menu (instances of commands are shared
             * among the existing menus)
             */
            foreach (List <CommandDescriptor> scopeCommands in PublicCommandsHelper.publicCommandsByScope.Values)
            {
                foreach (CommandDescriptor commandDescriptor in scopeCommands)
                {
                    guiControllerCommand guiC       = new guiControllerCommand();
                    CommandDescriptor    descriptor = commandDescriptor;
                    guiC.ControllerCommandFactoryMethod =
                        delegate
                    {
                        return(CommandSerializer.CreateCommandObject(descriptor.CommandType));
                    };
                    guiC.ControllerCommandType        = commandDescriptor.CommandType;
                    guiC.ControllerCommandDescription = commandDescriptor.CommandDescription;
                    guiCommandsForControllerCommands[commandDescriptor.CommandType] = guiC;
                }
            }

            foreach (Type t in typeof(guiScopeCommand).Assembly.GetTypes())
            {
                if (t.IsSubclassOf(typeof(guiScopeCommand)))
                {
                    ScopeAttribute a = (ScopeAttribute)t.GetCustomAttributes(typeof(ScopeAttribute), true).FirstOrDefault();
                    if (a != null)
                    {
                        #if SILVERLIGHT
                        foreach (ScopeAttribute.EScope scope in EnumHelper.GetValues(typeof(ScopeAttribute.EScope)))
                        #else
                        foreach (ScopeAttribute.EScope scope in Enum.GetValues(typeof(ScopeAttribute.EScope)))
                        #endif
                        {
                            if (scope == ScopeAttribute.EScope.None)
                            {
                                continue;
                            }
                            if (a.Scope.HasFlag(scope))
                            {
                                localCommandsByScope.CreateSubCollectionIfNeeded(scope);
                                localCommandsByScope[scope].Add((guiScopeCommand)t.GetConstructor(Type.EmptyTypes).Invoke(null));
                            }
                        }
                        if (a.Scope == ScopeAttribute.EScope.None)
                        {
                            localCommandsByScope.CreateSubCollectionIfNeeded(a.Scope);
                            localCommandsByScope[a.Scope].Add((guiScopeCommand)t.GetConstructor(Type.EmptyTypes).Invoke(null));
                        }
                    }
                }
            }
        }