public override bool Equals(object obj) { if (!base.Equals(obj)) { return(false); } if (obj == null) { return(false); } if (this.GetType() != obj.GetType()) { return(false); } AccountsAttribute other = obj as AccountsAttribute; if (other.m_accounts != m_accounts) { return(false); } return(true); }
public static void CanWriteCheck(object obj) { //构造Attribute类型的一个实例,并初始化 用于显示查找的内容 Attribute checking = new AccountsAttribute(Accounts.Checking); //构造应用于类型的Attribute实例 Attribute validAccounts = Attribute.GetCustomAttribute(obj.GetType(), typeof(AccountsAttribute), false); //如果Attribute应用于类型而且Attribute指定了, “Checking”账户,表明该类型可以开支票 if (validAccounts != null && checking.Match(validAccounts)) { Console.WriteLine("{0} types can write checks", obj.GetType()); } else { Console.WriteLine("{0} types can not write checks", obj.GetType()); } }