CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2007
    Posts
    5

    Find class by string

    Hey Guys,

    I have a serialized object stored in my DB, and it can be one of 3 classes (all of which implement the same interface). When I get the XML back from the DB, I also get a string containing the name of the class.

    I need to get the class from that string, such that I can use:

    new XmlSerializer(typeof(CLASS_NAME_HERE));

    I know in Java (my main language) I can ask the class loader for a class for a given string.

    Thoughts?

  2. #2
    Join Date
    Jun 1999
    Posts
    153

    Re: Find class by string

    From an old (.NET 1.1) code snippet I have:

    Code:
    Assembly assembly = Assembly.GetExecutingAssembly();
    
    // Find out from config file which ContinentFactory to create
    string continentFactory = ConfigurationSettings.AppSettings["ContinentFactory"];
    
    // Create ContinentFactory instance
    Type continentType = assembly.GetType(continentFactory);
    continentFactory contains the fully qualified class name. Once you've got the Type instance you should be able to pass it to your XmlSerilaizer.

    As you've already got your string fromthe DB you should be able to just use the first and last lines above.
    Kevin

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

    Re: Find class by string

    that will work only if the type you need is located in the exe (Assembly.GetExecutingAssembly returns the executable assembly only). if the type is loaded in from another dll, you'll have to iterate through all the assemblies in the app domain and do the assembly.GetType("your type") until it returns something that isn't null.

  4. #4
    Join Date
    Jun 1999
    Posts
    153

    Re: Find class by string

    Thanks for that correction.
    Kevin

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

    Re: Find class by string

    sorry, I wasn't trying to correct anything. poor choice of wording on my part... I should have said:

    In addition to whats been shown, if the type is located in another dll the type load will fail. If thats the case, you'll have to iterate through ...

  6. #6
    Join Date
    Jun 1999
    Posts
    153

    Re: Find class by string

    Yes. Basically, I just posted the first bit of code I could find that made use of a call to Assembly.GetType(). Hopefully this should be enough for the OP to be getting on with. But elaboration does no harm.
    Kevin

  7. #7
    Join Date
    Sep 2007
    Posts
    5

    Re: Find class by string

    Kevin, thanks for your speedy response... it was exactly what I needed! You're a star!

    MadHatter, thanks for the heads up too. I *was* using an external library, however I didn't need to check the different assemblies, because the call to getType() was in the external library too!

    For anyone who stumbles across this in future, my final code was:

    Code:
                Assembly assembly = Assembly.GetExecutingAssembly();
                Type pluginType = assembly.GetType(pluginName);
    
                XmlSerializer serializer = new XmlSerializer(pluginType);
    
                MemoryStream memoryStream = new MemoryStream(ASCIIEncoding.UTF8.GetBytes( xmlPayload ));
                XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
    
                pluginObject = (pluginType)serializer.Deserialize(memoryStream);
    Thanks guys!!

    jwa

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