简体   繁体   English

有没有一种方法可以使用PostSharp在运行时更改属性?

[英]Is there a way to change attributes in runtime using postsharp?

What i am trying to do, is to translate an application that uses attributes to set text in controls. 我正在尝试做的是翻译一个使用属性在控件中设置文本的应用程序。 I was thinking about custom reources manager but attributes has to be hardcoded. 我在考虑自定义资源管理器,但是必须对属性进行硬编码。 My question is: 我的问题是:

Is there any way to change visible text set by an attribute using PostSharp and where are the attributes stored in runtime? 有什么方法可以使用PostSharp更改属性设置的可见文本,这些属性在运行时存储在哪里?

eg for code 例如代码

[DataMember]
[DisplayName("Mission description")]
[Description("Description of this mission")]
public string Description { get; set; }

What do i want to achive is to extract "Mission description" and "Description of this mission" to external file, translate it, and pass new translated values to Description String as an Attribute during execution of program. 我要实现的是将“任务描述”和“此任务的描述”提取到外部文件中,进行翻译,然后在程序执行期间将新的翻译后的值作为属性传递给Description String。

What i had to do was to create a class that inherits from System.ComponentModel.DisplayNameAttribute, name it "DisplayNameAttribute" to override parent class, and overwrite parent class constructor, "DisplayName" and "DisplayNameValue" properties. 我要做的是创建一个从System.ComponentModel.DisplayNameAttribute继承的类,将其命名为“ DisplayNameAttribute”以覆盖父类,并覆盖父类的构造函数“ DisplayName”和“ DisplayNameValue”属性。 Next I put my logic into DisplayNameValue getter. 接下来,我将逻辑放入DisplayNameValue getter中。 Then create DescriptionAttribute class by analogy. 然后通过类推创建DescriptionAttribute类。

public class DisplayNameAttribute : System.ComponentModel.DisplayNameAttributes
{
        private string name;

        public DisplayNameAttribute() { }
        public DisplayNameAttribute(String name) { this.name = name; }

        public override string DisplayName 
        { 
            get 
            { 
                return DisplayNameValue;
            } 
        }

        public string DisplayNameValue
        {
            get
            {
                /* e.g logic for reading from dictionary file */
                return myDictionary[name];
            }
            set
            {
                name = value;
            }
        }
    }
}

Where "string name" is where i hold my key to Dictionary. 在这里,“字符串名称”是我按住字典的键的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM