I have this scenario, where I have a base class Book, which has two children FictionBook and NonFictionBook, both of which are overriding the ToString method. Currently, these classes are within a WCF Service Solution and I am getting a List of Books (List) on the client, however, I am stuck when attempting to utilise the .ToString() method of the child classes.
This is the code from the Client:
BookServiceClient bookService = new BookServiceClient("BasicHttpBinding_IBookService");
List<Book> matchingBooks = new List<Book>();
matchingBooks = (bookService.SearchBookByTitle(upperBookTitle)).ToList();
if (matchingBooks.Count > 0)
{
int count = 1;
foreach (Book book in matchingBooks)
{
Console.WriteLine("Matching Book " + count + ": " + book.ToString());
count++;
}
} else
{
Console.WriteLine("No matching books found");
}
This is one of the child classes within the Service which has overridden the ToString method. The other child class NonFictionBook is also overriding the ToString method and needs to be accessible from the client:
namespace BookModels
{
[DataContract]
class FictionBook : Book
{
[OperationContract]
public override string ToString()
{
return string.Format("Title: {0}, ISBN: {1}, Author: {2}", title, isbn, author);
}
}
}
Therefore, in a nutshell, I would like to access the ToString method from the client which is calling the Service.
What is the best solution to achieve this?