开发者

How is ImmutableObjectAttribute used?

开发者 https://www.devze.com 2022-12-10 06:37 出处:网络
I was looking for a built-in attribute to specify that a type is immutable, and I found only System.ComponentModel.ImmutableObjectAttribute.

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

Using Reflector, I checked where it was used, and it seems that the only public class that u开发者_如何学JAVAses it is System.Drawing.Image... WTF? It could have been used on string, int or any of the primitive types, but Image is definitely not immutable, there are plenty of ways to alter its internal state (using a Graphics or the Bitmap.SetPixel method for instance).

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


The documentation states:

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.

Reflector shows that the only method using this attribute is the internal System.Windows.Forms.PropertyGridInternal.GridEntry.Flags property getter used by design property grids.


I think you have the usage of ImmutableObjectAttribute confused -- it means that the object's properties should not be editable in a form designer or similar design-time UI, not that the object is itself immutable.

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


Look at the documentation for ImmutableObjectAttribute: "Specifies that an object has no subproperties capable of being edited... 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 this attribute isn't really about immutability: it's about disabling the display/editability of subproperties in an editor like a PropertyGrid.

So Image isn't being declared as immutable, not Int32 as mutable. Int32 doesn't need ImmutableObjectAttribute because it isn't expandable anyway. Image has it because it would be expandable, but not usefully so. It's just a really, really misleading name.

0

精彩评论

暂无评论...
验证码 换一张
取 消