Trying to use an excpetion class which could provide location reference for XML parsing, found an interesting behavior – compiler could not choose between overload which consumes an interface and one which needs System.Exception when I trying to pass XmlReader as a parameter.
Detais are following:
//exception overloads: public FilterXmlParseException(string message, Exception innerException) : base(message, innerException) { } public FilterXmlParseException(string message, IXmlLineInfo lineInfo) {...} //Usage: XmlReader reader = ... IXmlLineInfo lineinfo = (IXmlLineInfo)reader; //fails throw new FilterXmlParseException('<Filter> element expected', reader); //ok throw new FilterXmlParseException('<Filter> element expected', lineinfo);
And it fails since it could not select correct overload.But why? We see that XmlReader supports an interface and it is not inherited from System.Exception
The line:
because XmlReader doesn’t implement IXmlLineInfo. I am not sure if your cast works, but the casts are not checked statically. If it actually works, it is because the concrete class (that inherits from XmlReader) implements this interface, but the compiler has no way to know it.