本文整理汇总了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();
}