Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 158167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:38:01+00:00 2026-05-11T10:38:01+00:00

I’m using LINQ-to-XML to query a file for a list of objects. They all

  • 0

I’m using LINQ-to-XML to query a file for a list of objects. They all have the same structure (a set number of options, a list of parameters of indeterminate length). Based on their name in the XML file, I want to use the parameters for entirely different things – so I made subclasses of this object.

My question is – how do I invoke the correct constructor dynamically, without doing something like a case statement? Or does the solution to elegantly instantiating different subclasses from XML lie elsewhere?

class Object1 {     string name;     OptionObj opt;     List<Param> parameters; }  class sonOfObject : Object1 {     public void ExecuteSomething()     {          //do something with the parameters     } }  class secondSonOfObject : Object1 {     public void ExecuteSomething()     {          //do something entirely different with the parameters     } }  var query = from x in xmlDoc.Descendants('Object')     select new Object     {         //select the object from the XML     } 

After this bit of code I could use a copy constructor to make a sonOfObject from the more generic query object. But how do I do that dynamically, based on the name of the child class?

I know that you can get MethodInfo from a type and invoke it ala…

MethodInfo m = t.GetMethod(someName); m.Invoke(yourObj, null); 

But that would mean I would have to instantiate 9 or 10 of these child class objects (and get the type for each of them), and still ends up in some kind of case statement.

Is there a more elegant way to dynamically invoke child class constructors?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:38:02+00:00Added an answer on May 11, 2026 at 10:38 am

    In order for a class to participate in this scheme, I’d mark it with a custom attribute, which gives it an ‘XML name’.

    Then use reflection to find all the Types marked with that attribute and put them in a Dictionary<string, Type>. At this point you need to decide how you’re going to find other assemblies that might contribute types to the system. A simple approach is to look for other files with the extension .DLL in the same directory.

    Then whenever you have string from the XML that you want to use to create an instance of the corresponding Type, just look the Type up in the Dictionary by name and call Type.GetConstructor and ConstructorInfo.Invoke.

    The advantage of this is that the identifier in the XML is separated from the physical layout of your code – you can move types around in assemblies or rename them without invalidating old XML files.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have just tried to save a simple *.rtf file with some websites and
In my XML file chapters tag has more chapter tag.i need to display chapters
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload
I have a reasonable size flat file database of text documents mostly saved in
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.