CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2010
    Posts
    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

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  3. #3
    Join Date
    Feb 2010
    Posts
    1

    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?

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  5. #5
    Join Date
    May 2007
    Posts
    1,546

    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.

  6. #6
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    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.

  7. #7
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  8. #8
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: unboxing object to List<T>

    Quote Originally Posted by watzup_007 View Post
    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.

  9. #9
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    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
  •  





Click Here to Expand Forum to Full Width

Featured