[英]How to create a C# cmdlet that supports dot notation
I'm wondering if it's possible to create a cmdlet in C# that would provide properties that could be used in the PowerShell environment. 我想知道是否有可能在C#中创建一个cmdlet,该cmdlet将提供可在PowerShell环境中使用的属性。 I'm currently using PowerShell 3.0. 我当前正在使用PowerShell 3.0。
In PowerShell I'm looking for this kind of usage: 在PowerShell中,我正在寻找这种用法:
cmdlet.Property.WrappedObject.ItsProperties cmdlet.Property.WrappedObject.ItsProperties
In PowerShell, a cmdlet is of the form Verb-Noun . 在PowerShell中,cmdlet的形式为Verb-Noun。 It does not have a dot notation. 它没有点符号。 However, a cmdlet can return any .NET object as a result. 但是,cmdlet可以返回任何.NET对象。 So you can have a C# class such as: 因此,您可以拥有一个C#类,例如:
public class MyObject
{
public string ObjectName { get; set; }
public int ObjectValue { get; set; }
}
You can then have a PowerShell cmdlet, Get-MyObject, that returns that .NET object, such as: 然后,您可以拥有一个PowerShell cmdlet Get-MyObject,该cmdlet返回该.NET对象,例如:
$obj = Get-MyObject
And you can then use dot notation to get to the properties within the return object: 然后,您可以使用点表示法来获取返回对象内的属性:
Write-Host $obj.ObjectName
Write-Host $obj.ObjectValue
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.