How is ImmutableObjectAttribute used?

I was looking for a built-in attribute to indicate that the type is immutable, and I found only System.ComponentModel.ImmutableObjectAttribute .

Using a Reflector, I checked where it was used, and it seems that the only public class that uses it is System.Drawing.Image ... WTF? It could be used for string, int, or any of the primitive types, but Image is definitely not immutable, there are many ways to change its internal state (for example, using the Graphics method or Bitmap.SetPixel).

Thus, the only class in BCL that is explicitly declared immutable is mutable! Or am I missing something?

+10
immutability
source share
3 answers

The documentation states:

This attribute is typically used in the Properties window to determine how to render an extensible object as read-only. Therefore, this property is used only during development.

The reflector shows that the only method using this attribute is the internal attribute of the System.Windows.Forms.PropertyGridInternal.GridEntry.Flags property used by the project property grids.

+10
source share

I think that you have mixed up the use of ImmutableObjectAttribute - this means that the properties of the object should not be edited in the form designer or similar user interface during development, and not that the object itself is immutable.

This attribute is probably a candidate for the Daily WTF, however ...

+5
source share

See the documentation for ImmutableObjectAttribute: โ€œIndicates that the object does not have subprocesses that can be edited ... This attribute is usually used in the Properties window to determine whether the extensible object should be rendered as read-only. This property is used only in development time. " Thus, this attribute is not related to immutability: it disables the display / editing of subprocesses in an editor such as PropertyGrid.

Thus, the image is not declared immutable, and not Int32 as mutable. Int32 does not need the ImmutableObjectAttribute attribute because it does not expand anyway. He has an image because it will be extensible, but not useful. Itโ€™s just really, really a misleading name.

+3
source share

All Articles