Print Page | Close Window

Minor bug with Entity Key

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3326
Printed Date: 20-Apr-2024 at 12:27pm


Topic: Minor bug with Entity Key
Posted By: stephenmcd1
Subject: Minor bug with Entity Key
Date Posted: 08-Mar-2012 at 3:21pm
If you pass in an array of value types to the constructor of Entity Key, you get an exception.  For example:
var myKeyValues = new [] {123, 456}; //Compiler decides this should be an int[]
var key = new EntityKey(typeof(MyEntity), myKeyValues);
The EntityKey constructor will throw an exception like:
System.InvalidCastException: Unable to cast object of type 'System.Int32[]' to type 'System.Object[]'.
   at IdeaBlade.EntityModel.EntityKey..ctor(Type entityType, Object aValue, Boolean convertValue)
Looking at it in Reflector, it seems that constructor assumes that if the passed in value is an Array, it can safely cast it to an object array (object[]).  But that is only true if the source array is of reference types (which is not the case in my example because I'm passing in an array of ints).

The workaround for this is pretty easy but I think it's still worth fixing (we just ran into this in a part of our code that doesn't get hit very often so we 'forgot' to use the workaround).  For reference, the following code will work without a problem.
var myKeyValues = new object[] {123, 456}; // Force boxing of the ints to workaround Dev Force issue
var key = new EntityKey(typeof(MyEntity), myKeyValues);



Replies:
Posted By: DenisK
Date Posted: 09-Mar-2012 at 12:28pm
Hi stephenmcd1,

Thank you for the feedback. I'll file a bug report for this.



Print Page | Close Window