How to have an instance of anonyme class from reflection?
I have this string :
"<object name='hello' id='654' />"
with an indetermined attribute parameters number(and name)
from this I want to have an instance of anonyme class with a mapping between attribute and properties, and attributes values and properties values.
var anObject = new{name="hello",id=654};
I took the xml format for the string to ilustrate my words but I need a solution from any string format(I don’t want to Xml Serialisation).
For Reed Copsey (please make abstraction of the context where I need it).
I plan to use this to inject DefaultRoute to the map route of an asp.net MVC 3 application
routes.MapRoute(
"",
"superweirdurlwithnostructure.whatever",
new { controller = "Home", action = "Products", id = 500 }
);
referering to my question File to define route in MVC3
You can’t make an anonymous class this way (at least not without major hoops using something like CodeDom to actually write and compile code).
That being said, even if you could do this, there would be no good way to use this class. Anonymous types are really intended for use within the function where they are defined, and they are turned into a full class which is generated by the compiler at compile time.
One option, potentially, would be to use
dynamic:You could also use this via
ExpandoObject‘sIDictionary<string,object>interface:However, you’re still going to need a good way to use this – without knowing how the code should access the variables, building types dynamically doesn’t help solve many issues, and just creates more of them…