Определите пользовательский класс Attribute, чтобы сделать свойство Browsable в некоторых условиях - PullRequest
1 голос
/ 11 января 2020

У меня есть несколько классов в моем проекте, некоторые свойства 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 
}

Ответы [ 2 ]

1 голос
/ 11 января 2020

Это не то, что вы можете сделать с помощью атрибутов, поскольку BrowsableAttribute - это sealed. Чтобы сделать это через привязки , вам потребуется собственный дескриптор типа - так что вам нужно будет реализовать ICustomTypeDescriptor для вашего типа (напрямую или через TypeDescriptionProvider), предоставить пользовательский PropertyDescriptor, и измените способ просмотра там (IsBrowsable, IIR C).

Что составляет тонна работы.

Откровенно почти во всех случаях было бы лучше просто взять ручное управление привязками и добавить столбец / ввод / что угодно только после того, как вы проверили уровень безопасности.

0 голосов
/ 14 января 2020

Я определил свой собственный AdminBrowsable класс, используя Browsable код класса:

[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;
    }
}
...