|
-
February 20th, 2010, 03:26 PM
#1
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
-
February 20th, 2010, 08:39 PM
#2
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.
-
February 20th, 2010, 10:54 PM
#3
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?
-
February 21st, 2010, 02:32 AM
#4
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.
Last edited by BigEd781; February 21st, 2010 at 02:34 AM.
-
February 21st, 2010, 07:37 AM
#5
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.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
February 21st, 2010, 07:39 AM
#6
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.
-
February 21st, 2010, 07:59 AM
#7
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.
-
February 22nd, 2010, 04:36 AM
#8
Re: unboxing object to List<T>
 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.
-
February 25th, 2010, 01:43 PM
#9
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
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|