Skip to main content
improved formatting
Source Link

What I have is a collection of classes that all implement the same interface but can be pretty wildly different under the hood. II want to have a config file control which of the classes go into the collection upon starting the program, taking something that looks like :

<class1 prop1="foo" prop2="bar"/> 

and turning that into :

blah = new class1(); blah.prop1="foo"; blah.prop2="bar"; 

inIn a very generic way. The thing I don't know how to do is take the string "prop1"prop1 in the config file and turn that into the actual property accessor in the code. Are there any metaprogrammingmeta-programming facilities in C# to allow that?

What I have is a collection of classes that all implement the same interface but can be pretty wildly different under the hood. I want to have a config file control which of the classes go into the collection upon starting the program, taking something that looks like

<class1 prop1="foo" prop2="bar"/> 

and turning that into

blah = new class1(); blah.prop1="foo"; blah.prop2="bar"; 

in a very generic way. The thing I don't know how to do is take the string "prop1" in the config file and turn that into the actual property accessor in the code. Are there any metaprogramming facilities in C# to allow that?

What I have is a collection of classes that all implement the same interface but can be pretty wildly different under the hood. I want to have a config file control which of the classes go into the collection upon starting the program, taking something that looks like :

<class1 prop1="foo" prop2="bar"/> 

and turning that into :

blah = new class1(); blah.prop1="foo"; blah.prop2="bar"; 

In a very generic way. The thing I don't know how to do is take the string prop1 in the config file and turn that into the actual property accessor in the code. Are there any meta-programming facilities in C# to allow that?

Source Link
C Hogg
  • 1k
  • 10
  • 15

C#: instantiating classes from XML

What I have is a collection of classes that all implement the same interface but can be pretty wildly different under the hood. I want to have a config file control which of the classes go into the collection upon starting the program, taking something that looks like

<class1 prop1="foo" prop2="bar"/> 

and turning that into

blah = new class1(); blah.prop1="foo"; blah.prop2="bar"; 

in a very generic way. The thing I don't know how to do is take the string "prop1" in the config file and turn that into the actual property accessor in the code. Are there any metaprogramming facilities in C# to allow that?