To convert a derived class to a base class, you can use "upcast" (note that this cannot fail!):

1
2
3
4
> let rnd = new System.Random();;
val rnd : System.Random
> let o = (rnd :> obj);;
val o : obj

To do it the other way, you can use "downcast" (which can fail!):

1
2
3
4
5
6
7
8
 
> let rndAgain = (o :?> System.Random);;
val rndAgain : System.Random
> let rndAgain = (o :?> System.DateTime);; // this fails 
val rndAgain : System.DateTime
System.InvalidCastException: Specified cast is not valid.
   at <StartupCode$FSI_0010>.$FSI_0010._main()
stopped due to error
By on 10/8/2008 2:49 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper