private static void CanWriteCheck(Object obj) { Attribute checking = new AccountsAttribute(Accounts.Checking); Attribute validAccounts = obj.GetType().GetCustomAttribute <AccountsAttribute>(false); 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()); } }
private 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("{0} types can write checks.", obj.GetType()); } else { Console.WriteLine("{0} types can NOT write checks.", obj.GetType()); } }
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) { return(false); } return(true); }
private static void CanWriteCheck(Object obj) { // Construct an instance of the attribute type and initialize it // to what we are explicitly looking for. Attribute checking = new AccountsAttribute(Accounts.Checking); // Construct the attribute instance that was applied to the type Attribute validAccounts = obj.GetType().GetCustomAttribute <AccountsAttribute>(false); // If the attribute was applied to the type AND the // attribute specifies the "Checking" account, then the // type can write a check 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()); } }
public override Boolean Equals(Object obj) { // If the base class implements Equals and the base class // is not Object, then uncomment the line below. // if (!base.Equals(obj)) return false; // Since ‘this’ isn’t null, if obj is null, // then the objects can’t be equal // NOTE: This line may be deleted if you trust // the base type implemented Equals correctly. if (obj == null) { return(false); } // If the objects are of different types, they can’t be equal // NOTE: This line may be deleted if you trust // the base type implemented Equals correctly. if (this.GetType() != obj.GetType()) { return(false); } // Cast obj to our type to access fields. NOTE: This cast // can't fail since we know objects are of the same type AccountsAttribute other = (AccountsAttribute)obj; // Compare the fields to see if they have the same value // This example checks if 'this' accounts is the same // as other's accounts if (other.m_accounts != m_accounts) { return(false); } return(true); // Objects are equal }
public override Boolean Match(Object obj) { // If the base class implements Match and the base class // is not Attribute, then uncomment the line below. // if (!base.Match(obj)) return false; // Since ‘this’ isn’t null, if obj is null, // then the objects can’t match // NOTE: This line may be deleted if you trust // the base type implemented Match correctly. if (obj == null) { return(false); } // If the objects are of different types, they can’t match // NOTE: This line may be deleted if you trust // the base type implemented Match correctly. if (this.GetType() != obj.GetType()) { return(false); } // Cast obj to our type to access fields. NOTE: This cast // can't fail since we know objects are of the same type AccountsAttribute other = (AccountsAttribute)obj; // Compare the fields as you see fit // This example checks if 'this' accounts is a subset // of other's accounts if ((other.m_accounts & m_accounts) != m_accounts) { return(false); } return(true); // Objects match }
private static void CanWriteCheck(Object obj) { // Construct an instance of the attribute type and initialize it // to what we are explicitly looking for. Attribute checking = new AccountsAttribute(Accounts.Checking); // Construct the attribute instance that was applied to the type Attribute validAccounts = obj.GetType().GetCustomAttribute<AccountsAttribute>(false); // If the attribute was applied to the type AND the // attribute specifies the "Checking" account, then the // type can write a check 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()); } }