Question

Using the CreateInstance method of the TypeConverter class, it's possible to edit the properties of an immutable object - such as a font.

However, unlike the font class, there's some properties in my class that i'd like to be browsable but readonly - even though CreateInstance is supported.

Is there an attribute that supports this?

ETA: I've answered the question below. There's still room for a slight improvement, though, if anyone has any ideas.

Was it helpful?

Solution 2

To make a property readonly, inherit the type converter and override the CanConvertFrom method. Eg:

Public Class ReadOnlyStringConverter
    Inherits StringConverter

    Public Overrides Function CanConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal sourceType As System.Type) As Boolean
        Return (sourceType IsNot GetType(String)) AndAlso MyBase.CanConvertFrom(context, sourceType)
    End Function

End Class

The only down side is that the text does not appear read-only, so one might expect to be able to edit it.

If it's a one off, it's fine to nest the class and make it private so that it doesn't clutter up your intellisense.

To disable an editor, add the following attribute:

<Editor(GetType(UITypeEditor), GetType(UITypeEditor))> _

It's not enough to add:

<Editor()> _

If you do, then the existing editor will still be picked up.

OTHER TIPS

TypeConverter.CreateInstance() does not change the properties of, say, the Font class. It just creates a new instance of it. There's no magic here, it just uses the class constructor.

Just omit the property setter, you'll be fine.

If you want to prevent anybody from using Reflection to poke your private fields then you'll need to use the [ReflectionPermission] attribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top