Click to See Complete Forum and Search --> : unboxing object to List<T>


watzup_007
February 20th, 2010, 02:26 PM
Hi, I'm having problems in casting object to List<Game>, Game is another class. I'm using Reflection to invoke the method, however my method return a List, but the invokeMethod in Reflection returns me an object.

object returnType;

List<Game> gameList = (List<Game>)returnType;

When i tried to do this casting, it gives InvalidCastException. I've used, getType to check they are of the same type of list, however it cannot works. When i tried to compare them, i.e returnType.GetType().Equals(gameList.getType()), they are different.

Thanks for any help/suggestion. I've tried to search but failed to find any help. T.T

BigEd781
February 20th, 2010, 07:39 PM
As long as the object is of the type List<Game> you would not receive an exception, so the answer is that you are casting to the wrong type, that simple.

watzup_007
February 20th, 2010, 09:54 PM
Oh! is it because of different assemblies? i want to invoke the class during runtime, so i compile my project to a library and used reflection to load that assembly and invoke the method. However, inside my project i have not added the assembly. is this what may be causing the problem? how do i resolve?

BigEd781
February 21st, 2010, 01:32 AM
Yes, if assembly A does not know of the type "Game" it cannot create an instance of it as it does not know how. That said, your code should not even compile if the type is not defined because you are declaring an array of Game objects.

Mutant_Fruit
February 21st, 2010, 06:37 AM
If you have two different physical assemblies and they both declare identical classes in the same namespace, they are counted as being two completely different types. This is why your cast is failing.

MadHatter
February 21st, 2010, 06:39 AM
I've run across the exception cannot cast type Foo.A to Foo.A before, when the first instance was version 1.0 and the second was 1.1. do a GetType().AssemblyQualifiedName and compare those. chances are that if both are indeed List<Game> that one is a different version than the other.

BigEd781
February 21st, 2010, 06:59 AM
Also, just as a side note; the term "unboxing" refers to "boxing" a value type into a reference type. There is no unboxing when casting a reference type.

nelo
February 22nd, 2010, 03:36 AM
Oh! is it because of different assemblies? i want to invoke the class during runtime, so i compile my project to a library and used reflection to load that assembly and invoke the method. However, inside my project i have not added the assembly. is this what may be causing the problem? how do i resolve?

As stated by Mutant_Fruit the two types are not the same in the .NET world. The best way to solve it would be to package the class in a separate library that both your assemblies can reference.

JonnyPoet
February 25th, 2010, 12:43 PM
Excuse me but, when handling such things like binding a Type to a project when the project is already done, why you do not use a simple interface instead of that complicated way
If you have an Interface IGame containing all the methods properties,,, you will need from your class, Then it should be easy to compile the projects and bindin the class whenever needed. And in truth you may ad any sort of game class, as long it contains the interface IGame. Your project does not contain the class itself, it only contains and works with the interface of the class