ImmutableObject属性有什么好处? [英] What benefit does the ImmutableObject attribute provide?

查看:78
本文介绍了ImmutableObject属性有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是出于好奇心而测试ImmutableObjectAttribute属性,看看是否可以通过应用它获得一些好处,或者仅仅是出于语义修饰...

I was testing the ImmutableObjectAttribute attribute just for curiosity to see if I could gain some beneffits applying it, or if it was just for semantic decoration...

指定对象没有能够编辑的子属性.

Specifies that an object has no subproperties capable of being edited.

所以我有这节课:

<ImmutableObject(True)>
Public Class TestClass

    <ImmutableObject(True)>
    Public sb As StringBuilder

    Public Sub New()
        sb = New StringBuilder
    End Sub

End Class

我已经用以下代码进行了测试:

And I've tested with this code:

    Dim obj As New TestClass

    obj.sb.Append("Hello")
    obj.sb.Append(" ")
    obj.sb.Append("World")

    MsgBox(obj.sb.ToString)

然后,在可变对象中应用了ImmutableObject属性后,我希望看到某种编译器警告,某种运行时异常,或者只是从,但是所有事情都没有发生,一切似乎都可以正常进行.

Then, after I'd applied the ImmutableObject attribute in a mutable object, I expected to see some kind of compiler warning, some sort of runtime exception, or just receive an unexpected value from the internal string of the StringBuilder, but nothing of that things happened, all seems to work as normally.

这使我想到了一个答案似乎很明显的问题,但我需要问一下:

That makes me think a question whose answer seems obvious, but I need to ask it:

  • ImmutableObject属性仅仅是装饰性属性吗?
  • Is the ImmutableObject attribute just a decorative attribute?.

那么为什么存在这个属性?我找不到用该属性标记类的任何好处,它不能确保成员真正是不可变的,或者至少不能确保.Net理解为不可变的(您知道,就像String在.Net中并不是真正不变的那样).

Why exists this attribute then? I cannot find any beneffit marking a class with this attribute it it not ensures that the members are really immutable, or at least what .Net understands for immutable (you know, like a String is not really immutable in .Net).

推荐答案

来自

此属性通常在属性"窗口中用于确定 是否将可扩展对象呈现为只读.因此,此 属性仅在设计时使用.

This attribute is typically used in the Properties window to determine whether to render an expandable object as read-only. As such, this property is used only at design time.

...所以是的,这仅用于装饰.

... so yes, this is only for decoration.

这篇关于ImmutableObject属性有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆