【问题标题】:Adding or eliminating attribute at run time在运行时添加或删除属性
【发布时间】:2015-12-31 08:15:32
【问题描述】:

我有一个包含一堆属性的类。现在我将在运行时为这些属性添加或消除一些属性,但我不知道我应该如何做。

班级

Class myClass
{
    //Other codes
    public string DetailedAccountTitle
    {
        get { return detailedAccountTitle; }
        set { detailedAccountTitle = value; }
    }
    //Other codes
} 

调用者内部

if ((bool)e.Value)
{
    //Add the attribute
}
else
{
    //Eliminate it
}

现在你可以看到我正在控制调用者内部的一个事件,我希望它在它为真时添加属性,如果不是,则消除它。

【问题讨论】:

  • 您是否为此尝试过任何代码示例?
  • 旁注:你可以写成public string DetailedAccountTitle { get; set; }
  • @guy 我知道。反正 tnx :)
  • 您是否正在研究类似的内容:stackoverflow.com/questions/2483124/…?
  • 看看this

标签: c# attributes


【解决方案1】:

当您想在需要时使用属性值设置值并且不简单地设置为空或 null 时,我希望我能正确理解您的问题

myClass myclass = new myClass();

if ((bool)e.Value)
{
    myclass.DetailedAccountTitle = "Set Value";
}
else
{
    myclass.DetailedAccountTitle = String.Empty;
}

【讨论】:

  • 你根本没有得到这个问题。首先阅读:link
猜你喜欢
  • 2012-09-16
  • 2023-03-11
  • 2012-02-24
  • 1970-01-01
  • 2016-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多