static void CanWriteCheck(Object obj) { Attribute checking = new AccountsAttribute(Accounts.Checking); Attribute validAccounts = Attribute.GetCustomAttribute(obj.GetType(), typeof(AccountsAttribute), false); if (validAccounts != null && checking.Match(validAccounts)) { Console.WriteLine($"{obj.GetType()} types can write checks."); } else { Console.WriteLine($"{obj.GetType()} types can NOT write checks."); } }
public override bool Equals(object obj) { if (obj == null) { return(false); } if (this.GetType() != obj.GetType()) { return(false); } AccountsAttribute other = (AccountsAttribute)obj; if ((other.m_accounts & m_accounts) != m_accounts) { return(false); } return(true); }
public override bool Match(object obj) { //由于this 不为null, 如果obj=null,那么对象肯定不相等 if (obj == null) { return(false); } //如果对象的类型不一样,则肯定不相等 if (this.GetType() != obj.GetType()) { return(false); } AccountsAttribute other = (AccountsAttribute)obj; if ((other.m_accounts & m_accounts) != m_accounts) { return(false); } return(true); }