开发者

PowerShell Add-Type not behaving as expected

开发者 https://www.devze.com 2023-04-10 17:19 出处:网络
Can anyone explain why the following (using the System namespace qualifer) works: Add-Type @\" public class BitValueChecker

Can anyone explain why the following (using the System namespace qualifer) works:

Add-Type @"
public class BitValueChecker
{
    public static bool IsBitSetZeroBase开发者_如何学God(uint value, uint bitNumber)
    {
        if (bitNumber < 0 || bitNumber >= 32)
            throw new System.Exception("Invalid bit number must be >= 0 and <= 31");

        uint checkValue = value & System.Convert.ToUInt32(System.Math.Pow(2, bitNumber));
        return checkValue > 0;
    }
}
"@

while the below (essentially identical) snippet causes PS to complain that Exception, Convert and Math "do not exist in the current context"?

Add-Type @"
public class BitValueChecker
{
    public static bool IsBitSetZeroBased(uint value, uint bitNumber)
    {
        if (bitNumber < 0 || bitNumber >= 32)
            throw new Exception("Invalid bit number must be >= 0 and <= 31");

        uint checkValue = value & Convert.ToUInt32(Math.Pow(2, bitNumber));
        return checkValue > 0;
    }
}
"@


In your second code you have to add

Add-Type @"
using System;

public class BitValueChecker
{
....

Like in a c# code.

0

精彩评论

暂无评论...
验证码 换一张
取 消