C# 是否可以在属性上动态设置[DesignerSerializationVisibility]属性?

C# 是否可以在属性上动态设置[DesignerSerializationVisibility]属性?,c#,winforms,visual-studio-2013,C#,Winforms,Visual Studio 2013,我正在使用Visual Studio 2013和 我有以下枚举和属性: public enum MoreButtonIcon { Default, Calendar, Clock } [DefaultValue(false)] public bool UseCustomButtonIcon { get; set; } public MoreButtonIcon CustomButtonIcon { get; set; } privat

我正在使用Visual Studio 2013和 我有以下枚举和属性:

public enum MoreButtonIcon
{
    Default,
    Calendar,
    Clock
}

[DefaultValue(false)]
public bool UseCustomButtonIcon
{
    get;
    set;
}

public MoreButtonIcon CustomButtonIcon
{
    get;
    set;
}
private MoreButtonIcon customButtonIcon;
public MoreButtonIcon CustomButtonIcon
{
  get { return this.customButtonIcon; }
  set
  {
    if(!this.UseCustomButtonIcon)
      throw new Exception("UseCustomButtonIcon is false!");
    this.customButtonIcon = value;
  }
}
我希望属性“CustomButtonIcon”仅在“UseCustomButtonIcon”为true时出现在设计器中。否则应隐藏或至少禁用“自定义按钮图标”

是否可以设置: [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
属性上的属性?我假定您正在创建一个自定义控件。既然如此,你不能动态地,或者换言之,用设计器的术语——运行时来做。但您可以做的是,在CustomButtonNicon属性上添加一个检查:

    enum MoreButtonIcon
    {
        Default,
        Calendar,
        Clock,
        UNKNOWN
    }

    private MoreButtonIcon _customButtonIcon = MoreButtonIcon.UNKNOWN;
    public MoreButtonIcon CustomButtonIcon
    {
        get => _customButtonIcon;
        set
        {
            _customButtonIcon = UseCustomButtonIcon ? value : MoreButtonIcon.UNKNOWN;
            if (DesignMode) Refresh();
        }
    }

不,这是不可能的,但您可以向属性添加验证:

public enum MoreButtonIcon
{
    Default,
    Calendar,
    Clock
}

[DefaultValue(false)]
public bool UseCustomButtonIcon
{
    get;
    set;
}

public MoreButtonIcon CustomButtonIcon
{
    get;
    set;
}
private MoreButtonIcon customButtonIcon;
public MoreButtonIcon CustomButtonIcon
{
  get { return this.customButtonIcon; }
  set
  {
    if(!this.UseCustomButtonIcon)
      throw new Exception("UseCustomButtonIcon is false!");
    this.customButtonIcon = value;
  }
}