Hi I'm not sure a generic method is the right way to solve my problem. I need to parse an XML file and read items from it. Items can be things like orderLines, notes, attachments. The basic steps to get these items are all the same. How can I make 1 method which creates a list of these items and call a specific method to read an item?
public override IList<T> GetItems<T>(XPathNavigator currentOrder) where T : ISortableByLineNumber, new ()
{
var itemList = new List<T>();
var itemXmlNodes = currentOrder.Select(OrderXPath);
if (itemXmlNodes == null)
throw new Exception("");
var lineNumber = 1;
foreach (XPathNavigator itemXmlNode in itemXmlNodes)
{
var item = new T();
item = ReadItem(itemXmlNode, lineNumber++, item);
itemList.Add(item);
Logger.Debug(string.Format("Added item {0}", item));
}
return itemList;
}
I thought I could do this with the ReadItem method. I would create overloads for each type of item I would be reading.
private ISortableByLineNumber ReadItem(XPathNavigator itemXmlNode, int i, OrderLine item)
{
// specific code to read a orderLine
}
private ISortableByLineNumber ReadItem(XPathNavigator itemXmlNode, int i, Note item)
{
// specific code to read a note
}
But when I try to compile this could I get "The best overloaded method match for 'XmlOrderParser.XmlOrders.Prs3XmlFileWithOrders.ReadItem(System.Xml.XPath.XPathNavigator, int, XmlOrderParser.Entities.OrderLine)' has some invalid arguments". The problem is the compiler doesn't know how to cast T to OrderLine or Note.