當前位置: 首頁>>代碼示例>>C#>>正文


C# PSObject.First方法代碼示例

本文整理匯總了C#中System.Management.Automation.PSObject.First方法的典型用法代碼示例。如果您正苦於以下問題:C# PSObject.First方法的具體用法?C# PSObject.First怎麽用?C# PSObject.First使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Management.Automation.PSObject的用法示例。


在下文中一共展示了PSObject.First方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: NewItem

        /// <summary>
        /// processes the new-item cmdlet for paths that resolve to the appdomain value factory
        /// 
        /// adds an assembly to the current appdomain.  assemblies can be specified using
        /// their (partial) names or a file path.
        /// 
        /// this implementation essentially delegates all work to to the add-type built-in 
        /// powershell cmdlet to load an assembly from a partial name or file path.
        /// </summary>
        /// <param name="providerContext">the cmdlet providerContext providing powershell and framework services</param>
        /// <param name="path">the relative path supplied to the new-item cmdlet, either as the child element of the -path parameter, or the value of the -name parameter</param>
        /// <param name="itemTypeName">the type of the item to create; unused in this value</param>
        /// <param name="newItemValue">the value of the new item to create; unused in this value</param>
        /// <returns></returns>
        public IPathValue NewItem(IProviderContext providerContext, string path, string itemTypeName, object newItemValue)
        {
            IEnumerable<PSObject> results = new PSObject[]{};

            // first try to load the new assembly from an assembly name
            //  note that the invoked script will return a single assembly object if successful
            try
            {
                results = providerContext.InvokeCommand.InvokeScript(
                    String.Format("add-type -assemblyname '{0}' -passthru | select -first 1 -exp Assembly", path),
                    null);
            }
            catch
            {
            }

            // ... if that fails, try and load the assembly from a file path
            //  note that the invoked script will return a single assembly object if successful
            if( ! results.Any() )
            {
                try
                {
                    results = providerContext.InvokeCommand.InvokeScript(
                        String.Format("add-type -path '{0}' -passthru | select -first 1 -exp Assembly", path),
                        null);
                }
                catch
                {
                }
            }

            // raise any errors
            if( ! results.Any() )
            {
                throw new ArgumentException( "the specified name is not recognized as a valid assembly name or path");
            }

            // return the path value value to write to the pipeline
            var assembly = results.First().BaseObject as Assembly;

            // to maintain consistency I find it easier to leverage the value factory classes rather than return
            //  an IPathValue instance directly.
            var nodeFactory = new AssemblyPathNode(assembly);
            return nodeFactory.GetNodeValue();
        }
開發者ID:SpotLabsNET,項目名稱:p2f,代碼行數:59,代碼來源:AppDomainPathNode.cs


注:本文中的System.Management.Automation.PSObject.First方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。