I have a BankAccount class. FixedBankAccount and SavingsBankAccount derived from it.
I need to throw an exception if the resulting object is not a derived object. I have the following code.
IEnumerable<DBML_Project.BankAccount> accounts = AccountRepository.GetAllAccountsForUser(userId); foreach (DBML_Project.BankAccount acc in accounts) { string typeResult = Convert.ToString(acc.GetType()); string baseValue = Convert.ToString(typeof(DBML_Project.BankAccount)); if (String.Equals(typeResult, baseValue)) { throw new Exception("Not correct derived type"); } } namespace DBML_Project { public partial class BankAccount {
Is there any better code than this?
source share