`
huhu_long
  • 浏览: 71403 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Set attributes at runtime

    博客分类:
  • .net
阅读更多
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.
分享到:
评论

相关推荐

    DBGridPro new

    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...

    最新Ehlib 5.2.84(含完整源代码,支持delphi XE)

    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: ...

    Mastering_Perl_Tk_1st_ed_2002.pdf

    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 ...

    IOS5 Programming Cookbook

    - **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. ...

    JSP Simple Examples

    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 ...

    Python Cookbook, 2nd Edition

    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 ...

    CUDA Reference from NVIDIA official site.

    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 ...

    Bochs - The cross platform IA-32 (x86) emulator

    - 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 ...

    NETCFSERUP

    <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 ...

    UNIX环境高级编程英文第三版+源码

    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 ...

    Delphi7.1 Update

    * 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 ...

    Clever Internet Suite (SRC) v9.1.0.0

    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 ...

    Effective C#

    - **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; ...

    Dundas.Chart.for.Winform.Enterprise.v7.1.0.1812.for.VS2008

    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 ...

    spring-boot-reference.pdf

    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 ...

    Visual C++ 编程资源大全(英文源码 表单)

    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 一...

Global site tag (gtag.js) - Google Analytics