【问题标题】:Define a custom Attribute class to make a property Browsable in some conditions定义一个自定义 Attribute 类以使属性在某些条件下可浏览
【发布时间】:2020-01-11 08:32:14
【问题描述】:

我的项目中有一些类,一些属性是Browsable(false),所以用户看不到它们:

public class OrderEntity{
    public int Id { get; set;}   
    [Browsable(false)]
    public int ProductId { get; set;}   
    ....
}

我想,如果最终用户是管理员,他可以看到ProductId,但其他用户看不到。

所以我需要这样的东西:

public class OrderEntity{
    public int Id { get; set;}   
    [CustomizedBrowsable(false)]
    public int ProductId { get; set;}   
    ....
}


public class CustomizedBrowsable: Attribute
{
  if(AppContext.UserCode == "Admin") // The current user code saved in a static variable AppContext.UserCode.
    //do somethings   
  else
    //do somethings else 
}

【问题讨论】:

    标签: c# winforms attributes browsable


    【解决方案1】:

    这不是您可以通过属性执行的操作,因为 BrowsableAttributesealed。要做到这一点通过绑定,你需要一个自定义类型描述符 - 所以你需要在你的类型上实现ICustomTypeDescriptor(直接或通过TypeDescriptionProvider),提供自定义PropertyDescriptor ,并更改那里IsBrowsable,IIRC)的可浏览性定义方式。

    这是的工作。

    坦率地说,在几乎所有情况下,最好只手动控制绑定,并且只在检查安全级别后添加列/输入/任何内容。

    【讨论】:

      【解决方案2】:

      我使用Browsable 类代码定义了我自己的AdminBrowsable 类:

      [AttributeUsage(AttributeTargets.All)]
      public sealed class AdminBrowsableAttribute : Attribute
      {
          /// <summary>
          /// Specifies that a property or event can be modified at design time. This static field is read-only.
          /// </summary>
          public static readonly AdminBrowsableAttribute Yes = new AdminBrowsableAttribute(true);
          /// <summary>
          /// Specifies that a property or event cannot be modified at design time. This static field is read-only.
          /// </summary>
          public static readonly AdminBrowsableAttribute No = new AdminBrowsableAttribute(false);
          /// <summary>
          /// Specifies the default value for the <see cref="T:System.ComponentModel.BrowsableAttribute"/>, which is <see cref="F:System.ComponentModel.BrowsableAttribute.Yes"/>. This static field is read-only.
          /// </summary>
          public static readonly AdminBrowsableAttribute Default = AdminBrowsableAttribute.Yes;
          private bool browsable = true;
      
          /// <summary>
          /// Gets a value indicating whether an object is browsable.
          /// </summary>
          /// 
          /// <returns>
          /// true if the object is browsable; otherwise, false.
          /// </returns>
          public bool Browsable
          {
              get
              {
                  return this.browsable;
              }
          }
      
          /// <summary>
          /// Initializes a new instance of the <see cref="T:System.ComponentModel.BrowsableAttribute"/> class.
          /// </summary>
          /// <param name="browsable">true if a property or event can be modified at design time; otherwise, false. The default is true. </param>
          public AdminBrowsableAttribute(bool browsable)
          {
              if (!AppContext.IsAdmin)
              {
                  this.browsable = browsable;
              }
          }
      
          /// <summary>
          /// Indicates whether this instance and a specified object are equal.
          /// </summary>
          /// 
          /// <returns>
          /// true if <paramref name="obj"/> is equal to this instance; otherwise, false.
          /// </returns>
          /// <param name="obj">Another object to compare to. </param>
          public override bool Equals(object obj)
          {
              if (obj == this)
                  return true;
              AdminBrowsableAttribute browsableAttribute = obj as AdminBrowsableAttribute;
              if (browsableAttribute != null)
                  return browsableAttribute.Browsable == this.browsable;
              return false;
          }
      
          /// <summary>
          /// Returns the hash code for this instance.
          /// </summary>
          /// 
          /// <returns>
          /// A 32-bit signed integer hash code.
          /// </returns>
          public override int GetHashCode()
          {
              return this.browsable.GetHashCode();
          }
      
          /// <summary>
          /// Determines if this attribute is the default.
          /// </summary>
          /// 
          /// <returns>
          /// true if the attribute is the default value for this attribute class; otherwise, false.
          /// </returns>
          public override bool IsDefaultAttribute()
          {
              return this.Equals((object)AdminBrowsableAttribute.Default);
          }
      }
      

      我只改了Constructor

      public AdminBrowsableAttribute(bool browsable)
      {
          if (AppContext.UserCode != "Admin")
          {
              this.browsable = browsable;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2019-07-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多