gpt4 book ai didi

C# 属性或特性

转载 作者:太空宇宙 更新时间:2023-11-03 18:39:48 25 4
gpt4 key购买 nike

我知道您可以像这样在 C# 中向方法添加属性,

ex1.

[HttpPost]
public void Method()
{
//code
}

这意味着必须满足该属性才能运行Method()

我知道你可以像这样堆叠属性,

ex2.

[HttpPost]
[RequireHttps]
public void Method2()
{
//More code
}

在使用 Method2() 之前,它会检查 attribute1 'AND' attribute2 是否满足。

但是您可以“或”属性吗?也许是这样的?

ex3.

[HttpPost || RequireHttps]
public void Method3()
{
//Even more code
}

因此,如果满足任一属性,您可以使用 Method3()

编辑:抱歉,印象中的属性称为注释。解决了这个问题。

最佳答案

Which means the annotation must be satisfied to run Method()

这是误会。

[HttpPost]属性是一个指令,这个方法只会匹配一个Post请求。这不是像安全检查那样的“要求”。只有一些属性以这种方式工作。

但当将它们视为“需求”时:它们独立工作,因此始终会导致AND 行为。

关于C# 属性或特性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10211895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com