Hi,
the following code should work:
#light
let reader =
System.Xml.XmlReader.Create
(new System.IO.StringReader("<doc></doc>"))
let li = ((box reader) :?> System.Xml.IXmlLineInfo)
System.Console.WriteLine(li.LinePosition)
The problem that you probably had is that casting 'reader' directly to 'System.Xml.IXmlLineInfo' using 'reader :?> System.Xml.IXmlLineInfo' doesn't work - I'm not sure if my understanding is correct, but I would say that the :?> operator requires the target type to be inherited from the type of the value on the left side (so having B inherited from A and a instance of (static) type A you could write 'a :?> B'). For dynamic casts to interface types this may be an unnecessary restriction, but I'm not sure if there is any other good reason.
Anyway, the solution is converting the 'reader' to a type obj, which is a base for all .NET types and it is possible to use dynamic cast to any other type from the obj type. Converting any F# value to obj can be done using the 'box' function.
Tomas Petricek (
Blog), C# MVP
My book:
Real-world Functional Programming in .NET