How do I conditionally add a class with Add-Type -TypeDefinition if it isn't added already?

前端 未结 5 1274
我在风中等你
我在风中等你 2020-12-16 09:15

Consider the following PowerShell snippet:

$csharpString = @\"
using System;

public sealed class MyClass
{
    public MyClass() { }
    public override stri         


        
5条回答
  •  甜味超标
    2020-12-16 09:46

    There's a nicer way to do this without incurring the cost of exceptions:

    if (-not ("MyClass" -as [type])) {
        add-type @"
            public class MyClass { }
    "@
    }
    

    update: well, apparently powershell signals internally with an exception anyway. It has a bad habit of doing this. The interpreter uses SEH to signal with the break and continue keywords, for example.

提交回复
热议问题