コード例 #1
0
        public IPathNode NewItem(IContext context, string path, string itemTypeName, object value)
        {
            var validValueTypes = new[] {typeof (ScriptBlock), typeof (string)};
            if (!validValueTypes.Contains(value.GetType()))
            {
                var validNames = String.Join(", ", validValueTypes.ToList().ConvertAll(t => t.FullName).ToArray());
                throw new ArgumentException("new item values must be one of the following types: " + validNames);
            }

            AddIn addIn = Locator.Get<AddIn>();
            if( null == addIn )
            {
                throw new ServiceUnavailableException( typeof( AddIn ) );
            }

            var p = context.DynamicParameters as NewItemDynamicParameters ?? new NewItemDynamicParameters();
            object[] contextGuids = new object[] {};

            string functionName = FunctionUtilities.GetFunctionNameFromPath(path);
            
            var cmd = _commands.AddNamedCommand2(
                addIn,
                path,
                p.Label ?? path,
                p.Tooltip ?? String.Empty,
                true,
                null,
                ref contextGuids,
                (int) vsCommandStatus.vsCommandStatusSupported + (int) vsCommandStatus.vsCommandStatusEnabled,
                (int) vsCommandStyle.vsCommandStylePictAndText,
                vsCommandControlType.vsCommandControlTypeButton
                );

            if (null != p.Bindings)
            {
                cmd.Bindings = p.Bindings;
            }

            string command = "new-item -path function:global:" + functionName + " -value {" + value + "} ";

            context.InvokeCommand.InvokeScript(command, false, PipelineResultTypes.None, null, null);
            
            var factory = new CommandNodeFactory(cmd);
            return factory.GetNodeValue();
        }