Yes, the ref as Type will return null if it cannot be converted to type (somewhat akin to C++'s dynamic_cast, with some differences); I was referring to the OP's code snippet: the explicit cast should fail and an InvalidCastException should be thrown?
public static Bitmap DecodeFile(string _path)
{
WeakReference reference;
var options = new BitmapFactory.Options {InPreferredConfig =
Bitmap.Config.Rgb565};
Bitmap image=null;
//refs is a dictionary of Dictionary<string, WeakReference>
if (refs.TryGetValue(_path, out reference))
{
if (reference.IsAlive)
{
image = (Bitmap)reference.Target; //
<<------------------- over here!
Regards,
Joel
> -----Original Message-----
> From: [email protected] [mailto:monodroid-
> [email protected]] On Behalf Of Jonathan Pryor
> Sent: Wednesday, 27 June, 2012 9:47 PM
> To: Discussions related to Mono for Android
> Subject: Re: [mono-android] Not catching null reference - why?
>
> On Jun 26, 2012, at 9:03 PM, Joel Low wrote:
> > Isn't the normal (= .NET) behaviour to throw an InvalidCastException in
this
> case?
>
> No. The `value as Type` expression will either return null or return a
non-null
> instance of Type. Type cannot be a value type.
>
> http://msdn.microsoft.com/en-us/library/cscsdfbt.aspx
>
> - Jon
>
> _______________________________________________
> Monodroid mailing list
> [email protected]
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________ Monodroid mailing list [email protected] UNSUBSCRIBE INFORMATION: http://lists.ximian.com/mailman/listinfo/monodroid
