Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# PSCmdlet动态自动完成参数(如Get进程)_C#_Powershell_Pscmdlet - Fatal编程技术网

C# PSCmdlet动态自动完成参数(如Get进程)

C# PSCmdlet动态自动完成参数(如Get进程),c#,powershell,pscmdlet,C#,Powershell,Pscmdlet,在powershell中,某些参数具有动态自动完成行为。 例如,获取进程参数名。我可以用TAB遍历所有进程 我想在我的PSCmdlet中使用此行为。 但问题是,我只知道如何使用静态自动完成值来实现这一点。请参见示例: public class TableDynamicParameters { [Parameter] [ValidateSet("Table1", "Table2")] public string[] Tables { get; set; } } 下面是如

在powershell中,某些参数具有动态自动完成行为。 例如,获取进程参数名。我可以用TAB遍历所有进程

我想在我的PSCmdlet中使用此行为。

但问题是,我只知道如何使用静态自动完成值来实现这一点。请参见示例:

public class TableDynamicParameters
{
    [Parameter]
    [ValidateSet("Table1", "Table2")]
    public string[] Tables { get; set; }
}
下面是如何使用本机powershell完成此操作的示例


它工作正常 thx至@bouvierr

public string[] Tables { get; set; }

public object GetDynamicParameters()
{
    if (!File.Exists(Path)) return null;

    var tableNames = new List<string>();
    if (TablesCache.ContainsKey(Path))
    {
        tableNames = TablesCache[Path];
    }
    else
    {
        try
        {
            tableNames = DbContext.GetTableNamesContent(Path);
            tableNames.Add("All");
            TablesCache.Add(Path, tableNames);
        }
        catch (Exception e){}
    }

    var runtimeDefinedParameterDictionary = new RuntimeDefinedParameterDictionary();
    runtimeDefinedParameterDictionary.Add("Tables", new RuntimeDefinedParameter("Tables", typeof(String), new Collection<Attribute>() { new ParameterAttribute(), new ValidateSetAttribute(tableNames.ToArray()) }));

    return runtimeDefinedParameterDictionary;
}
public string[]表{get;set;}
公共对象GetDynamicParameters()
{
如果(!File.Exists(Path))返回null;
var tableNames=新列表();
if(tableCache.ContainsKey(路径))
{
tableNames=tablecache[Path];
}
其他的
{
尝试
{
tableNames=DbContext.GetTableNamesContent(路径);
表名。添加(“全部”);
tablecache.Add(路径、表名);
}
捕获(例外e){}
}
var runtimeDefinedParameterDictionary=新的runtimeDefinedParameterDictionary();
runtimeDefinedParameterDictionary.Add(“Tables”,新RuntimeDefinedParameter(“Tables”,typeof(String),new Collection(){new ParameterAttribute(),new ValidateStatAttribute(tableNames.ToArray())});
返回runtimeDefinedParameterDictionary;
}
来自MSDN:

您的
Cmdlet
类必须实现
IDynamicParameters
接口。此接口:

提供cmdlet检索可由Windows PowerShell运行时动态添加的参数的机制

编辑:

IDynamicParameters.GetDynamicParameters()方法应:

返回一个对象,该对象的属性和字段具有与cmdlet类或RuntimeDefinedParameterDictionary对象中定义的属性类似的参数相关属性

如果你看这个,作者是在PowerShell中这样做的。他在运行时创建:

  • validateStatAttribute
    的一个新实例,具有一个运行时可能值数组
  • 然后,他创建一个
    RuntimeDefinedParameter
    ,并将
    validateStatAttribute分配给该参数
  • 他返回一个包含此参数的
    RuntimeDefinedParameterDictionary

在C#中也可以这样做。您的
GetDynamicParameters()
方法应返回此
RuntimeDefinedParameterDictionary
包含相应的
RuntimeDefinedParameter

这将允许在指定另一个参数时添加第二个参数。我看不出这如何回答OP关于动态确定参数的潜在值的问题。@Matt我澄清了我的答案。我发布的链接展示了这项技术。它在PS中,但都是.NET对象,因此您可以在C#中的
GetDynamicParameters()
方法中执行相同的操作。是指向同一事物的另一个SO帖子的链接。包含同一解决方案的脚本编制人员链接。