关于参数的PureAttribute的目的

Luk*_*oid 12 c# .net-4.0 code-contracts

我理解PureAttribute用于标记某些东西(类,方法,委托等)没有可见的变化,但我可以从以下定义中看出它可以应用于方法参数:

[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Constructor|AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Event|AttributeTargets.Parameter|AttributeTargets.Delegate, AllowMultiple = false, Inherited = true)]
public sealed class PureAttribute : Attribute
Run Code Online (Sandbox Code Playgroud)

应用于参数的此属性的用途是什么,如下所示:

public void SomeMethod([Pure]SomeClass theParameter)
{
}
Run Code Online (Sandbox Code Playgroud)

是否暗示不SomeMethod应该使用theParameter未标记为的任何内容,这[Pure]意味着我们可以确保SomeClass在调用之前和之后看起来相同的实例是相同的SomeMethod

我没有看到PureAttribute这种方式的使用,并想知道这是由于代码合同缺乏支持还是因为对我的误解?

Alb*_*reo 5

正如PureAttribute您所说, 表示类型或方法是纯类型或方法,也就是说,它不会进行任何可见的状态更改(直接取自 MSDN 文章)。

\n\n

也许你SomeClass没有被标记为纯净,因为它可以改变状态,但这并不意味着其中的一切都是不纯净的。

\n\n

也许你SomeMethod不使用任何SomeClass不纯的方法,也许它只是读取它的属性(我假设你没有在属性获取器中执行不纯的操作,否则你就是邪恶的),所以它的用法SomeClass是纯的。\xc2\ xad\xc2\xad\xc2\xad\xc2\xad\xc2\xad

\n

  • 我不是那个投反对票的人。然而,正如我的问题所述,我确实理解“PureAttribute”的目的并且几乎每天都使用它,我所追求的是对其应用于参数时的目的进行一些澄清。 (2认同)