小编Chr*_*ter的帖子

Cmdlet 实例持续多久?

对派生的 Cmdlet 的自定义实现会是什么样子感兴趣,真的想看看涉及多少工作。模拟了以下内容:

using System.Management.Automation;

namespace EMSEngineering
{
    [Cmdlet(VerbsDiagnostic.Test, "Cmdlet")]
    public class TestCmdlet : Cmdlet
    {
        static int nextIndex = 0;

        private int index = nextIndex;

        public TestCmdlet()
        {
            System.Console.WriteLine("instantiating {0}", index);
            nextIndex++;
        }

        ~ TestCmdlet()
        {
            System.Console.WriteLine("terminating {0}", index);
        }

        [Parameter(Mandatory = true, ValueFromPipeline = true)]
        public string[] SomeParameter { get; set; }

        protected override void BeginProcessing()
        {
            if (SomeParameter != null)
                WriteVerbose(string.Format("begin processing {0} params", SomeParameter.Length));
        }

        protected override void ProcessRecord()
        {
            foreach (string param in SomeParameter)
                WriteObject(string.Format("processing …
Run Code Online (Sandbox Code Playgroud)

powershell

3
推荐指数
1
解决办法
39
查看次数

什么是“<调用>d__40”?

使用 Powershell,System.Management.Automation.Cmdlet.Invoke() 返回类型的对象'<Invoke>d__40'而不是指定的 OutputType。

重现:

  1. SendGreeting 示例 cmdlet复制到 .\ExampleCmdlet.cs
  2. powershell -NoProfile
  3. Add-Type -Path .\ExampleCmdlet.cs
  4. $command = [SendGreeting.SendGreetingCommand]::new()
  5. $command.Name = 'Person'
  6. $invoke = $command.Invoke()
  7. $invoke.GetType()

预期[string]
实际[<Invoke>d__40]

$PS版本表:

Name                           Value
----                           -----
PSVersion                      5.1.19041.1237
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.1237
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
Run Code Online (Sandbox Code Playgroud)

Get-Member -InputObject $invoke揭示这个来实现IEnumerator和一些玩弄.MoveNext().Current有时会输出预期的"Hello Person!"结果。

这是什么<Invoke>d__40类型?
为什么$command.Invoke()不直接返回预期的字符串输出?

c# powershell powershell-cmdlet

3
推荐指数
1
解决办法
116
查看次数

标签 统计

powershell ×2

c# ×1

powershell-cmdlet ×1