1

i have a base Question class and lot of classes that derives from this class like CheckboxQuestion class and RadioButtonQuestion class

the program iterates over xml code for every block that is inside a question tag it will make a new element based on the tag's name and is a child class of the Question class

 <question number="1"> <RadioButtonQuestion> <title>What is the right ...</title> <choices> <choice value="1">answer 1</choice> <choice value="2">answer 2</choice> <choice value="3">answer 3</choice> <choice value="4">answer 4</choice> </choices> </RadioButtonQuestion> </question> 

after iterating on all the xml file i want to put all the questions in IEnumerable<Questions> the problem is that i don't know how to make a new class based on the text written in an xml document

3
  • 1
    This actually looks more like a case of deserialization than reflection... Commented Apr 23, 2011 at 1:11
  • @Etienne de Martel no its not the problem was based on text i want to make a new instance Commented Apr 23, 2011 at 1:14
  • This question is very unclear. I'm not sure what you're asking. Are you trying to create an instance of a class, given its name read from an XML file? Are you trying to create a new type derived from your Question class based on the XML schema? Clarifying this question, as well as cleaning up the capitalization and punctuation, would help you get more answers, and make it easier to find for people with similar problems :) Commented Apr 23, 2011 at 21:50

3 Answers 3

4

If the XML tag directly reflects the name of your class you can use Type.GetType() to determine the corresponding type - keep in mind that it needs the full namespace though ("Test" in the example below). Then you can use Activator.CreateInstance() to create an instance of that type.

string xmlTagName = "RadioButtonQuestion"; Type type = Type.GetType("Test." + xmlTagName ); var question = Activator.CreateInstance(type); 

Also keep in mind that Activator.CreateInstance() returns object. It might be better overall if you determined the right type depending on the tag name, and then instantiated an instance the old fashioned way.

Sign up to request clarification or add additional context in comments.

1 Comment

A sometimes-useful alternative to using Activator.CreateInstance is getting a constructor of the type using reflection, i.e.: var question = type.GetConstructor(<array of the constructor's parameter types>).Invoke(<array of argument values to pass to constructor>); One advantage this has over Activator.CreateInstance is that you can use private and internal constructors as well as public ones, simply by passing BindingFlags.NonPublic as the 2nd argument of type.GetConstructor. Just thought I'd mention that for future readers with similar problems :)
2

Assuming the types are all in the currently executing assembly, this should instantiate the class by name without needing the switch statement:

public object InstantiateClass(string name, params object[] args) { return Activator.CreateInstance(this.GetType().Assembly.FullName, name, args); } 

2 Comments

How is passing this.GetType().Assembly.FullName different from just passing in null?
It's not, I just prefer to be explicit so that the meaning is clear. If his types aren't in the currently executing assembly he can replace that with the correct assembly name.
0
 switch(input) { case optionA: return Activator.CreateInstance(typeof(Bla)); case optionB: return Activator.CreateInstance(typeof(Blb)); } 

$0.02

Any extra parameters to the constructor of Bla/Blo can be added

Activator.CreateInstance(typeof(Bla), 1, 2 ,3); 

Actually, it takes a params array of objects. So if you needed to pass them dynamically,

object[] dynparams = new object[] { 1,2, "more", 4.5, new List<int>() }; ... return Activator.CreateInstance(typeof(Bla), dynparams); 

Numerous other overloads exist, read about them here

Now I don't know any of your typenames, but you will be able to do

Object obj = Activator.CreateInstance(null, "Namespace1.MyClass1", dynparams); 

4 Comments

i dont want to make this switch statement i don't even want to specify the type. what i want is this string that you read consider it the class name and make a new instance of this class(string) and pass a value to the class constructor
Why would you ever use Activator.CreateInstance(typeof(Bla)) instead of new Bla()?
@Nadeem: read the overloads. It contains what you want. I just don't know the class names for you :) @Gabe: Because of course typeof(Bla()) could be anything there.
Woah! Just noticed twice down voted. All just because people won't think for themselves when pointed at the right API, in my not-so-humble opinion. Oh, well... :_

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.