Is it possible to apply EditableAttribute at runtime? I want to make some properties editable only when the user has some role. Unfortunately EdiatbleAttribute is sealed. I can try to apply it at runtime by reflection but maybe there's more proper way to do this. Thanks for any advices.
是否可以在运行时应用EditableAttribute ?我希望只有当用户具有某些角色时,才可以编辑一些属性。不幸的是EdiatbleAttribute密封。我可以尝试通过反射在运行时应用它但也许有更合适的方法。谢谢你的任何建议。
Best regards
致以最亲切的问候
2 个解决方案
#1
1
There is a hack available at: How to Set Attribute Value at Runtime-- and How to Work around a Silly Bug
这里有一个技巧:如何在运行时设置属性值——以及如何处理一个愚蠢的Bug
private void SetPropertyGrid()
{
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(typeof(Student))["Address"];
ReadOnlyAttribute attrib = (ReadOnlyAttribute)descriptor.Attributes[typeof(ReadOnlyAttribute)];
FieldInfo isReadOnly = attrib.GetType().GetField("isReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
isReadOnly.SetValue(attrib,true);
propertyGrid1.SelectedObject = new Student();
}
I was able to this code to change the ReadOnly
attribute value of the property. The statement propertyGrid1.SelectedObject = new Student();
can be replaced by propertyGrid1.SelectedObject = myStudent
i.e. you can modify the properties of an existing object.
我能够通过这段代码更改属性的ReadOnly属性值。声明propertyGrid1。SelectedObject =新学生();可以用propertyGrid1替换。SelectedObject = myStudent,即您可以修改现有对象的属性。
Also, have a look at a similar question: Change Attribute's parameter at runtime
同样,请查看一个类似的问题:在运行时更改属性的参数
#2
0
I think a good option is to create an extension helper for the control you need to use (TextBox etc) and if the IsReadOnly property is true (Editable(false)) then generate a disabled textbox.
我认为一个很好的选择是为需要使用的控件创建一个扩展助手(TextBox等),如果IsReadOnly属性为true (Editable(false)),则生成一个禁用的文本框。
#1
1
There is a hack available at: How to Set Attribute Value at Runtime-- and How to Work around a Silly Bug
这里有一个技巧:如何在运行时设置属性值——以及如何处理一个愚蠢的Bug
private void SetPropertyGrid()
{
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(typeof(Student))["Address"];
ReadOnlyAttribute attrib = (ReadOnlyAttribute)descriptor.Attributes[typeof(ReadOnlyAttribute)];
FieldInfo isReadOnly = attrib.GetType().GetField("isReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
isReadOnly.SetValue(attrib,true);
propertyGrid1.SelectedObject = new Student();
}
I was able to this code to change the ReadOnly
attribute value of the property. The statement propertyGrid1.SelectedObject = new Student();
can be replaced by propertyGrid1.SelectedObject = myStudent
i.e. you can modify the properties of an existing object.
我能够通过这段代码更改属性的ReadOnly属性值。声明propertyGrid1。SelectedObject =新学生();可以用propertyGrid1替换。SelectedObject = myStudent,即您可以修改现有对象的属性。
Also, have a look at a similar question: Change Attribute's parameter at runtime
同样,请查看一个类似的问题:在运行时更改属性的参数
#2
0
I think a good option is to create an extension helper for the control you need to use (TextBox etc) and if the IsReadOnly property is true (Editable(false)) then generate a disabled textbox.
我认为一个很好的选择是为需要使用的控件创建一个扩展助手(TextBox等),如果IsReadOnly属性为true (Editable(false)),则生成一个禁用的文本框。