PowerShell 添加类型未按预期运行
任何人都可以解释为什么以下(使用 System
命名空间限定符)有效:
Add-Type @"
public class BitValueChecker
{
public static bool IsBitSetZeroBased(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;
}
}
"@
而下面的(基本相同)代码片段会导致 PS 抱怨 Exception
、Convert 和
Math
“在当前上下文中不存在”?
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;
}
}
"@
Can anyone explain why the following (using the System
namespace qualifer) works:
Add-Type @"
public class BitValueChecker
{
public static bool IsBitSetZeroBased(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;
}
}
"@
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在第二个代码中,您必须
在 ac# 代码中添加 Like 。
In your second code you have to add
Like in a c# code.