powershell datacontext变量设置为false return true

发布于 2025-02-10 12:40:38 字数 736 浏览 3 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我早已燃尽 2025-02-17 12:40:38

json side

  • falsetrue - 注意引用的缺失 - boolean json values 表示其名称所暗示的值。

  • 相比之下, “ false”“ true”是json strings 其值发生要 falsetrue分别 - 字符串值具有没有内在含义,除了 by conduntion JSON文档的消费者必须知道。

    • (这样的惯例的一个示例是convertto-jsonconvertfrom-json使用特殊格式的字符串,例如“ 2022-06-27T15:31 :38.935539-04:00“表示日期/time [dateTime])值,以补偿缺少日期/时间数据类型JSON本身 - 参见这个答案

因此,您应该使用以下内容来创建一个对象,其myproperty value包含一个实际的布尔值 - 请注意,false is

$DataObject =  ConvertFrom-Json @"
{
    "myproperty": false
}
"@

通常,除 strings 外,它们始终是 double> double Quoted (例如,“ foo”), json支持以下无引用的原始数据类型作为值:

  • booleans:true> true 和false
  • null值:null
  • 数字:eg,42-421.0 /代码>和1E2
    • 注意: standard 不允许前缀+或.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 ' | ConvertFrom-Json).GetType().FullName   # -> System.Boolean
# vs. 
(' "false" ' | ConvertFrom-Json).GetType().FullName # -> System.String

:将这些值解释为布尔值的术语,这意味着:

  • 唯一 falsetrue值直接映射到其[bool ]对应(以$ false$ true表示为

    )。

  • 您观察到的, PowerShell在布尔上下文中评估字符串 时,它会考虑 它的内容 ,因此[bool]“ false”也得出$ true

    • 如果您正在处理要包含“ true”“ false”(或其任何情况变化)的字符串,并且您想对其进行解析作为布尔值,使用 <代码> [bool] :: parse() 方法;例如,[bool] :: parse(“ false”)产生$ false,按预期。

  • PowerShell通常会实现自己的自动to-Boolean转换 [1] (例如上面描述的), do /em>适用于.NET API

    • 鉴于.net具有 no 内置的字符串转换,它取决于dataContext键入以将字符串解释为布尔值,尽管使用了[bool] :: parse()是逻辑候选者(在C#语法中,例如:bool.parse(“ false”)))


[1]转换规则,请参阅此答案

On the JSON side:

  • false and true - 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 be false and true, respectively - a string value has no intrinsic meaning, except by convention that the consumer of a JSON document must be aware of.

    • (An example of such a convention is that ConvertTo-Json and ConvertFrom-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 that false is unquoted:

$DataObject =  ConvertFrom-Json @"
{
    "myproperty": false
}
"@

Generally, aside from strings, which are always double-quoted (e.g., "foo"), JSON supports the following unquoted primitive data types as values:

  • Booleans: true and false
  • Null values: null
  • Numbers: e.g., 42, -42, 1.0 and 1e2
    • Note: The standard doesn't permit prefix + or .1 as shorthand for 0.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):

(' false ' | ConvertFrom-Json).GetType().FullName   # -> System.Boolean
# vs. 
(' "false" ' | ConvertFrom-Json).GetType().FullName # -> System.String

In terms of interpreting these values as Booleans, this means:

  • Only unquoted false and true 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.

    • If you're dealing with string that you know to contain either "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.

    • Given that .NET has no built-in string-to-Boolean conversion, it is up to the 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文