开发者

PowerShell Add-Type not behaving as expected

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜