简体   繁体   English

是否可以在c#中的Powershell cmdlet之间共享属性和注释?

[英]Is it possible to share properties and comments between Powershell cmdlets in c#?

When exposing a set of related functions in Powershell cmdlets, is it possible to share the property names and summary help to normalize these across cmdlets in an assembly? 在Powershell cmdlet中公开一组相关功能时,是否可以共享属性名称和摘要帮助以在程序集中的各个cmdlet中对它们进行规范化?

I know that this can be done with derived classes, but this solution is awkward at best when there are multiple cmdlets with different properties to be shared. 我知道可以使用派生类来完成此操作,但是当要共享多个具有不同属性的cmdlet时,此解决方案最多是尴尬的。

Here is an extremely simple example. 这是一个非常简单的例子。 I would like to share the property 'Name' and all related comments so that they are the same across the N cmdlets we are producing, but I cannot think of a good way to do this in c#. 我想共享属性“名称”和所有相关注释,以使它们在我们正在生成的N个cmdlet中都相同,但是我想不出在c#中实现此目的的好方法。 Ideally any sharing would allow specification of Parameter attributes such as Mandatory or Position. 理想情况下,任何共享都将允许指定参数属性,例如强制性或位置性。

namespace FrozCmdlets
{
using System.Management.Automation;

/// <summary>
/// Adds a new froz to the system.
/// </summary>
[Cmdlet( VerbsCommon.Add, "Froz" )]
public class AddFroz : Cmdlet
{
    /// <summary>
    /// The name of the froz.
    /// For more information on the froz, see froz help manual.
    /// </summary>
    [Parameter]
    public string Name { get; set; }

    protected override void ProcessRecord()
    {
        base.ProcessRecord();
        // Add the froz here
    }
}

/// <summary>
/// Removes a froz from the system.
/// </summary>
[Cmdlet( VerbsCommon.Remove, "Froz" )]
public class RemoveFroz : Cmdlet
{
    /// <summary>
    /// The name of the froz.
    /// For more information on the froz, see froz help manual.
    /// </summary>
    [Parameter]
    public string Name { get; set; }

    protected override void ProcessRecord()
    {
        base.ProcessRecord();
        // Remove the froz here
    }
}
}

Yes, there is a way to do this without inheriting from a common base class for the parameters. 是的,有一种方法可以做到这一点而无需继承通用的参数基类。 Its not not well documented, only hinted at in the IDynamicParameters.GetDynamicParameters method's remarks. 它的记录不充分,仅在IDynamicParameters.GetDynamicParameters方法的注释中得到了提示。 Here's a more detailed treatment of the topic. 这是对该主题的更详细处理。

First, create a class with your common parameters declared as properties with the [Parameter] attributes: 首先,创建一个类,将您的通用参数声明为带有[Parameter]属性的属性:

internal class MyCommonParmeters
{
    [Parameter] 
    public string Foo { get; set; }
    [Parameter]
    public int Bar { get; set; }
    ...
}

Then each Cmdlet that wants to use these common parameters should implement the IDynamicParameters interface to return a member instance of the MyCommonParameters class: 然后,每个想要使用这些公共参数的Cmdlet都应实现IDynamicParameters接口以返回MyCommonParameters类的成员实例:

[Cmdlet(VerbsCommon.Add, "Froz")]
public class AddFroz : PSCmdlet, IDynamicParameters
{
    private MyCommonParmeters MyCommonParameters 
        = new MyCommonParmeters();

    object IDynamicParameters.GetDynamicParameters()
    {
        return this.MyCommonParameters;
    }
    ...

With this approach, the PowerShell command parameter binder will find and populate the parameters on the MyCommonParameters instance just as if they were members of the Cmdlet classes. 使用这种方法,PowerShell命令参数绑定器将在MyCommonParameters实例上查找并填充参数,就像它们是Cmdlet类的成员一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM