Click here to Skip to main content
15,888,208 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 attribute. I've also tried to get an answer in this forum, but after one day nobody has looked on this topic, so I ask here again (and last time)

Here are the properties I want to change:
C#
[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:
C#
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);



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.
Posted
Comments
OriginalGriff 10-Sep-11 3:46am    
If nobody answered last time, there is probably a good reason: Nobody knows the answer. Reposting the same question is just rude, and unhelpful given that it is now a Saturday, and you are even less likely to get a response that you were yesterday...
Aoi Karasu 10-Sep-11 6:33am    
isReadOnly.SetValue(attribute, Video); <-- does this even work? I cannot compile it. Shoudn't it be isReadOnly.SetValue(attribute, true); or isReadOnly.SetValue(attribute, false); ?
PotensGladius 14-Sep-11 13:27pm    
Video is a boolean parameter value

The video property has an Editor defined on it, and that editor is responsible for the readonly checking, try removing it.
 
Share this answer
 
Comments
PotensGladius 14-Sep-11 13:28pm    
I can't because I want a path browser for this property for the designer grid.
Hi,
Try to search in Google may be find it;

this code get it form google may be help u

------------------------
C#
class TestClass
{
    private bool isMyPropertyReadOnly;

    public bool IsMyPropertyReadOnly
    {
        get { return isMyPropertyReadOnly; }
        set { isMyPropertyReadOnly = value; }
    }

    private int myVar;

    public int MyProperty
    {
        get { return myVar; }

        set
        {
            if (isMyPropertyReadOnly)
            {
                throw new System.Exception("The MyProperty property is read-only.");
            }
            else
            {
                myVar = value;
            }
        }
    }
}
 
Share this answer
 
Comments
PotensGladius 14-Sep-11 13:29pm    
You're good. I've searched in google about feeled 5 hours and found nothing.
This does not compile for me:

isReadOnly.SetValue(attribute, Video);


This does compile but gives you incorrect behavior:

isReadOnly.SetValue(attribute, Volume);


Try it like this:

public class MenuProp
{
    // ... class content ...

	public void ChangeReadOnlyTo(bool readOnly)
	{
		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);

        	// Here you set the attribute value to true or false
		isReadOnly.SetValue(attribute, readOnly);

		property = TypeDescriptor.GetProperties(typeof(MenuProp))["Volume"];
		attribute = (ReadOnlyAttribute)property.Attributes[typeof(ReadOnlyAttribute)];
		isReadOnly = attribute.GetType().GetField("isReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);

		// Here you set the attribute value to true or false
		isReadOnly.SetValue(attribute, readOnly);
	}
}


Also make sure that UIPathEditor respects the value of the ReadOnlyAttribute.
 
Share this answer
 
v2
Comments
PotensGladius 14-Sep-11 13:26pm    
That was the point.
The UIPathEditor hasn't respected the readonly attribute
thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900