更改属性上的ReadOnly标志 [英] Change ReadOnly flag on property
问题描述
我试图通过反射更改只读属性.一切正常的属性运行良好.但是当我使用自定义编辑器时,它不会更改标志.
这是我要更改的属性:
I''ve tried to change a readonly attribute over reflection. Everything runs nice with the normal properties. But when I''m using a custom editor, it doesn''t change the flag.
Here are the properties I want to change:
[UIPathEditor.OfdParams("Video Files|*.wmv", "Choose Background Video")]
[Editor(typeof(UIPathEditor), typeof(UITypeEditor))]
[ReadOnly(false)]
[Description("The background video")]
[Category("Appearance")]
public string Video
{
get { return _backgroundVideo; }
set { _backgroundVideo = value; }
}
[ReadOnly(false)]
[Description("The volume of the video")]
[Category("Appearance")]
public int Volume
{
get { return _volume; }
set { if (value <= 100 && value >= 0)_volume = value;}
}
这是我用来执行此操作的代码:
This is the code I use to do this:
PropertyDescriptor property;
ReadOnlyAttribute attribute;
FieldInfo isReadOnly;
property = TypeDescriptor.GetProperties(typeof(MenuProp))["Video"];
attribute = (ReadOnlyAttribute)property.Attributes[typeof(ReadOnlyAttribute)];
isReadOnly = attribute.GetType().GetField("isReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
isReadOnly.SetValue(attribute, Video);
property = TypeDescriptor.GetProperties(typeof(MenuProp))["Volume"];
attribute = (ReadOnlyAttribute)property.Attributes[typeof(ReadOnlyAttribute)];
isReadOnly = attribute.GetType().GetField("isReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
isReadOnly.SetValue(attribute, Volume);
有了音量属性,它运行良好.但是在Video属性上没有任何作用...
我想更改 Attribute [ReadOnly(false)] ,因为它是针对要编辑这些属性的属性网格.而且我想根据所选对象在可编辑和不可编辑之间切换.
With the volume-property it runs fine. But on the Video property it has no effect...
I want to change the Attribute[ReadOnly(false)] because its for a property-grid where I want to edit those properties. And I want to switch between editable and non-editable depending on the choosen object.
推荐答案
至少有一个原因导致它原则上不起作用.您的isReadOnly
是一个字段,但是您在谈论的是属性.如果此字段是属性后面的字段,则对其进行修改通常无效.
考虑如何实现(通常以这种方式实现):字段isReadOnly
仅用于记住属性的当前值.典型的代码如下所示:
There is at least one reason why it might not work in principle. YourisReadOnly
is a field, but you''re talking about a property. If this field is a field behind the property, modification of it usually has no effect.
Think about how it can be implemented (and usually implemented this way): the fieldisReadOnly
is used only to remember current value of the property. The typical code looks like this:
public IsReadOnly {
get { return isReadOnly; }
set {
if (value == isReadOnly) return;
isReadOnly = value;
SetReadOnlyCore(value); //all real work is done here
}
}
bool isReadOnly;
所有实际工作都在设置员的最后完成.为了说明起见,我将其命名为对某些方法SetReadOnlyCore
的调用.它可能正在通过P/Invoke调用某些本机方法.
通过Reflection修改字段,您无需调用属性设置器.所以什么都没发生尝试修改属性本身,而不是字段.
All real work is done at the end of the setter; for illustration, I named is the call to some method SetReadOnlyCore
. It could be calling some native method via P/Invoke.
By modifying the field via Reflection you don''t call the property setter; so nothing happens. Try to modify the property itself, not a field.
这篇关于更改属性上的ReadOnly标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!