Changing a property's attribute at runtime is generally frowned upon.
After all, attributes are just metadata and should remain unchanged after they are compiled.
But sometimes you have to do it, regardless of what the "best practices" are saying.
In my project, there are a lot of tree view items share the same entity. But now the user want to add more properties for one of the view item. And we have to use the original entity because of business limits.
So we added these requested properties and display them in that view item. However, as you can image, all of other view items also displays this newly added properties.
We are not going change this in view layer. So the only choice would be change the attribute at runtime.
The attribute we are using is "BrowsableAttribute", which under namespace System.ComponentModel
Take below entity for example, we are going to hide "Customer" property...
public class Task
{
public int TaskID { get; set; }
...
public string Customer { get; set; }
}
And here is the code to set "Browsable" attribute to false, the default value is true.
var descriptor = TypeDescriptor.GetProperties(typeof(DisplayWorkflowTask))["Customer"];
var attribute = (BrowsableAttribute) descriptor.Attributes[typeof (BrowsableAttribute)];
var attributeFieldInfo = attribute.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
if(attributeFieldInfo != null) attributeFieldInfo.SetValue(attribute, false);
Yap, here we are.
分享到:
相关推荐
Multilevel layout of column titles - fields of dataset record can be represented as several rows in the grid to enhance the display of information to end-users at runtime. Column layout can be easily...
to add components on panel of detail information at RunTime. Use next code: MyControl.Parent := DBGridEh1.RowDetailPanelControl; + Next subproperties were added in propery Grid.STFilter: ...
This section focuses on dynamic font manipulation, demonstrating techniques for changing font attributes at runtime based on user input or other conditions. **3.5 Font Manipulation Methods** A ...
- **Do-While Loop**: A `do-while` loop is similar to a `while` loop but guarantees that the loop body will be executed at least once. The syntax is `do { /* code */ } while (condition);`. ### 11. ...
Runtime Errors Errors are arised when there is any logic problem with the logic of the program. Try catch in jsp In try block we write those code which can throw exception while code execution and ...
Processing a String One Character at a Time Recipe 1.2. Converting Between Characters and Numeric Codes Recipe 1.3. Testing Whether an Object Is String-like Recipe 1.4. Aligning Strings ...
The CUDA Runtime API is a core component of the CUDA platform and provides a set of functions for managing GPU resources, executing kernels, and performing other tasks essential to developing ...
- Major configure/cpu rework allowing to enable/disable CPU options at runtime through .bochsrc (Stanislav) - Bugfixes for CPU emulation correctness and stability - Implemented X2APIC extensions ...
<remarks>The API class provides the capability of "reflecting" through objects to gain information at runtime about their properties. This capability provides an excellent way for application ...
4.4 Set-User-ID and Set-Group-ID 98 4.5 File Access Per missions 99 4.6 Ownership of New Files and Directories 101 4.7 access and faccessat Functions 102 4.8 umask Function 104 4.9 chmod, fchmod, and ...
* Setting MTSDataModule.AutoComplete to False in the IDE is overridden and set back to True at run time (Quality Central 4716).* Calls to ApplyUpdates(-1) on a TMTSDataModule do not work properly ...
FTP Client - SSL / TLS support, the ability to set FTP file attributes and permissions were added. FTP FXP (Site to Site) transfer mode. FTP Server - custom events for all operations with files and ...
- **Usage:** Use `readonly` for values that are determined at runtime but should not change after initialization. For example: ```csharp public class Person { public readonly string Name; ...
When set to false and all points have an x-value of 0, the points will not index but all draw at 0 What’s new in Dundas Chart V7.0? Silverlight Add-On - Version 7 gives developers a novel wrapper ...
I. Spring Boot Documentation 1. About the Documentation 2. Getting Help 3. First Steps 4. Working with Spring Boot 5. Learning about Spring Boot Features 6. Moving to Production 7. Advanced Topics ...
55.zip Transparent Dialog 透明的对话框(5KB)<END><br>56,56.zip Viewing Dialog Template Resources at Runtime 运行时看对话框模板资源(5KB)<END><br>57,57.zip Alternative Wizard Dialog 一...