unboxing object to List<T>
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
Re: unboxing object to List<T>
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.
Re: unboxing object to List<T>
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?
Re: unboxing object to List<T>
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.
Re: unboxing object to List<T>
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.
Re: unboxing object to List<T>
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.
Re: unboxing object to List<T>
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.
Re: unboxing object to List<T>
Quote:
Originally Posted by
watzup_007
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.
Re: unboxing object to List<T>
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