[ACCEPTED]-Instantiating anonymous object using C# object initializer syntax stored in string-dynamic
Anonymous classes are C# syntactic sugar (see Remarks section here). csc.exe creates 12 a class with private fields and a read/write 11 property with the type inferred from context. All 10 uses of the object are, again, inferred.
What 9 this means is you cannot create an anonymous 8 class at run time because the CLR sees them 7 no differently than any other class (again, because 6 it is C# syntactic sugar).
So instead:
- Use a
Dictionary<string,object>
- Use JSON.NET, XML or something like it that has some already-defined scheme for parsing a string to get an object. This requires the properties be well-defined, however.
- Use
System.Reflection.Emit
to create the type at run-time, but I see no real benefit to this over just aDictionary<string,object>
I also 5 have concerns of what you're doing because 4 this as a string very likely means to me 3 that you are accepting user input of some 2 kind. Be wary of the security issues in 1 whatever you do.
It's not possible using anonymous types, however 2 you can do it with Reflection Emit using 1 the TypeBuilder class, specifically TypeBuilder.Create(..).
http://msdn.microsoft.com/en-us/library/system.reflection.emit.typebuilder.createtype.aspx
The best way to do it is to use serialization. But 11 of course, that doesn't use the same string 10 format that you described.
Your object initializer 9 does not have to contain constant values.
string myName="bob";
int myAge=30;
double mySalary=100000;
object empData = new { name = myName, age = myAge, salary = mySalary };
So 8 in your scenario you would have to parse 7 out the individual elements from your string, and 6 perform some conversions on them to coerce 5 them into the types you want.
If you are 4 not married to this particular string format, you 3 can serialize and deserialize your objects 2 and accomplish the same thing using XML 1 much more easily.
There's no direct easy way to do so. Basically, you 7 have these options:
- Parse the string manually.
- Use the C# compiler to compile that object as a part of an assembly and use reflection to figure out the contents.
- (not sure if it's possible, C# 4.0 only): Use C# 4.0 managed compiler classes to parse the expression and infer the stuff.
None of which is ideal 6 in my opinion. I'd consider QueryString 5 like pairs, XML or JSON or some other format 4 that has parsers available out there instead 3 if you have the option and consider storing 2 data in a dictionary instance instead of 1 creating an object for every expression.
I don't think this question answers yours 11 per se, but it will tell you why it's not 10 possible to create anonymous instances of 9 objects using strings in a manner such as 8 you suggest:
How do I create and access a new instance of an Anonymous Class passed as a parameter in C#?
Anonymous types don't have any 7 public fields, consequently while you can 6 do this for a named object, it's not so 5 simple to do it for an anonymous type. That 4 said, there's nothing to stop you [as suggested 3 by BFree] using reflection to emit the MSIL 2 for the anonymous type - which isn't exactly 1 straightforward, but isn't impossible either.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.