CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #3
    Join Date
    Nov 2007
    Location
    Birmingham, England
    Posts
    157

    Re: Can you discover at runtime what class <T> is?

    Quote Originally Posted by keang View Post
    The generics information is only available at compile time and not at runtime so your cast is effectively a cast to type Object and hence will never fail.
    Yes that's pretty much confirmed what I thought.

    I'm building a program with a plug-in architecture. Based on a configuration it will load objects of various classes named in a config file. Obviously the classes it loads must implement an interface specific to type of plug-in. The user could configure the program incorrectly, so it's important to catch the resulting class cast exception and handle it appropriately. I'm trying to wrap up the entire code for loading a plug-in into a generic function so that it does not have to be re-written for each type of plug-in.

    The flaw to all of this is that the calling code (not the plug-in loader) could generate a ClassCastException. Yes that's not impossible to fix but it's messy and ideally it should be caught or predicated and avoided by the plug-in loader.

    I think the plug-in loader function will have to take an extra java.lang.Class argument.
    Last edited by couling; November 15th, 2010 at 10:28 AM.
    Signature
    Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
    End Signature

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