powershell datacontext变量设置为false return true
我正在使用PowerShell应用程序中的DataContext(对我来说是第一个),并且可以从JSON设置所有DataContext属性和值,并且所有值都是字符串。示例:
$DataObject = ConvertFrom-Json @"
{
"myproperty":"false"
}
但是我有一个问题,那里的错误并不总是评估为false。当我将控件的启用属性绑定到myProperty时,它会在设置为false时正确禁用。
但是,如果我试图将该属性评估为布尔,它将返回真实:
If($state.myproperty){$true} Else {$false}
True
出于控件的目的,我不明白为什么它将其作为布尔值false寄存,但是我无法在代码中引用它。
请让我了解我缺少的东西。
编辑我已经了解PowerShell将在0个字符上注册为true的任何字符串。但是,我仍然很好奇为什么当绑定到控制属性时,这似乎不是一个问题。
edit2 我实际上没有在JSON中设置默认值,而只是将$ state.myproperty设置为$ false,然后在脚本开始时将其设置为$ true。
再次仍然很好奇控件绑定为什么正确评估字符串“ false”。另外,如果我想在JSON中初始化此功能,您可以在不使用报价的情况下这样做吗?例如“ myproperty”:false?
I'm using a datacontext in my powershell app (a first for me) and from what I can tell all datacontext properties and values are set with JSON and all values are strings. Example:
$DataObject = ConvertFrom-Json @"
{
"myproperty":"false"
}
But I'm having an issue where that false up there is not always evaluation to false. When I bind a control's enable property to myproperty, it properly disables as it's set to false.
But if I try to evaluate that property as a bool, it returns true:
If($state.myproperty){$true} Else {$false}
True
I don't understand why it registers as a boolean false, for purposes of the control, but I cannot reference it as such in code.
Please school me on what I'm missing.
EDIT I've come to understand Powershell will register any string over 0 characters as true. However, I'm still curious as to why when bound to a control property, this doesn't seem to be an issue.
EDIT2 I worked around this by not actually setting a default value in the JSON and simply setting the $state.myproperty to $false near the beginning of the script and set it to $true when my condition required.
Again still curious as to why the control binding properly evaluates the string "false". Also if I wanted to initialize this in JSON, can you do so without use of quotes? E.g. "myproperty":false ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 json side :
false
和true
- 注意引用的缺失 - boolean json values 表示其名称所暗示的值。相比之下,
“ false”
和“ true”
是json strings 其值发生要false
和true
分别 - 字符串值具有没有内在含义,除了 by conduntion JSON文档的消费者必须知道。convertto-json
和convertfrom-json
使用特殊格式的字符串,例如“ 2022-06-27T15:31 :38.935539-04:00“
表示日期/time ([dateTime]
)值,以补偿缺少日期/时间数据类型JSON本身 - 参见这个答案)因此,您应该使用以下内容来创建一个对象,其
myproperty
value包含一个实际的布尔值 - 请注意,false
is :通常,除 strings 外,它们始终是 double> double Quoted (例如,
“ foo”
), json支持以下无引用的原始数据类型作为值:true> true 和
false
null
42
,-42
,1.0
/代码>和- 注意: standard 不允许前缀
1E2
+或
.1
作为0.1
的速记,仅支持DECIMAL 数字表示;但是,特定的实现可能不太严格。在powershell/.net side :
powershell的
convertfrom-json
将这些值映射到其类似的.NET数据类型,[string]
(system.string
)和[bool]
((<) a href =“ https://learn.microsoft.com/en-us/dotnet/api/system.boolean” rel =“ nofollow noreferrer”>system.boolean.boolean
):将这些值解释为布尔值的术语,这意味着:
唯一
false
和true
值直接映射到其[bool ]
对应(以$ false
和$ true
表示为)。
您观察到的, PowerShell在布尔上下文中评估字符串 时,它会考虑 它的内容 ,因此
[bool]“ false”
也得出$ true
。“ true”
或“ false”
(或其任何情况变化)的字符串,并且您想对其进行解析作为布尔值,使用 <代码> [bool] :: parse() 方法;例如,[bool] :: parse(“ false”)
产生$ false
,按预期。PowerShell通常会实现自己的自动to-Boolean转换 [1] (例如上面描述的), do /em>适用于.NET API 。
dataContext
键入以将字符串解释为布尔值,尽管使用了[bool] :: parse()
是逻辑候选者(在C#语法中,例如:bool.parse(“ false”)
))[1]转换规则,请参阅此答案。
On the JSON side:
false
andtrue
- note the absence of quoting - are Boolean JSON values that represent the values their names imply.By contrast,
"false"
and"true"
are JSON strings whose value happens to befalse
andtrue
, respectively - a string value has no intrinsic meaning, except by convention that the consumer of a JSON document must be aware of.ConvertTo-Json
andConvertFrom-Json
use specially formatted strings such as"2022-06-27T15:31:38.935539-04:00"
to represent date/time ([datetime]
) values, to compensate for the absence of a date/time data type in JSON itself - see this answer)Thus, you should use the following to create an object whose
.myproperty
value contains an actual Boolean value - note thatfalse
is unquoted:Generally, aside from strings, which are always double-quoted (e.g.,
"foo"
), JSON supports the following unquoted primitive data types as values:true
andfalse
null
42
,-42
,1.0
and1e2
+
or.1
as shorthand for0.1
, and supports decimal number representations only; specific implementations, however, may be less strict.On the PowerShell / .NET side:
PowerShell's
ConvertFrom-Json
maps these values to their analogous .NET data types,[string]
(System.String
) and[bool]
(System.Boolean
):In terms of interpreting these values as Booleans, this means:
Only unquoted
false
andtrue
values map directly onto their[bool]
counterparts (expressed as$false
and$true
in PowerShell).As you've observed, when PowerShell evaluates a string in a Boolean context, it considers any non-empty string
$true
, irrespective of its content, so that[bool] "false"
yields$true
as well."true"
or"false"
(or any case variation thereof) and you want to parse it as a Boolean, use the[bool]::Parse()
method; e.g.,[bool]::Parse("false")
yields$false
, as intended.PowerShell generally implements its own, automatic to-Boolean conversions[1] (such as the one described above), which do not apply to .NET APIs.
DataContext
type to interpret strings as Booleans, though the use of[bool]::Parse()
is a logical candidate (in C# syntax, e.g.:bool.Parse("false")
)[1] For a summary of the conversion rules, see the bottom section of this answer.