Reputation: 9580
Using the C# object initializer syntax I can instantiate an anonymous object like this:
object empData = new { name = "bob", age = 30, salary = 100000 };
But what if I have the initializer stored in a string, e.g.:
string init = "{ name = \"bob\", age = 30, salary = 100000 }";
Whats the best way of converting this string into an instance of the object?
Upvotes: 15
Views: 20796
Reputation: 11528
Anonymous classes are C# syntactic sugar (see Remarks section here). csc.exe creates a class with private fields and a read/write property with the type inferred from context. All uses of the object are, again, inferred.
What this means is you cannot create an anonymous class at run time because the CLR sees them no differently than any other class (again, because it is C# syntactic sugar).
So instead:
Dictionary<string,object>
System.Reflection.Emit
to create the type at run-time, but I see no real benefit to this over just a Dictionary<string,object>
I also have concerns of what you're doing because this as a string very likely means to me that you are accepting user input of some kind. Be wary of the security issues in whatever you do.
Upvotes: 22
Reputation: 180868
The best way to do it is to use serialization. But of course, that doesn't use the same string format that you described.
Your object initializer 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 in your scenario you would have to parse out the individual elements from your string, and perform some conversions on them to coerce them into the types you want.
If you are not married to this particular string format, you can serialize and deserialize your objects and accomplish the same thing using XML much more easily.
Upvotes: 5
Reputation: 39846
I don't think this question answers yours per se, but it will tell you why it's not possible to create anonymous instances of objects using strings in a manner such as 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 public fields, consequently while you can do this for a named object, it's not so simple to do it for an anonymous type. That said, there's nothing to stop you [as suggested by BFree] using reflection to emit the MSIL for the anonymous type - which isn't exactly straightforward, but isn't impossible either.
Upvotes: 1
Reputation: 103750
It's not possible using anonymous types, however you can do it with Reflection Emit using the TypeBuilder class, specifically TypeBuilder.Create(..).
http://msdn.microsoft.com/en-us/library/system.reflection.emit.typebuilder.createtype.aspx
Upvotes: 5
Reputation: 422132
There's no direct easy way to do so. Basically, you have these options:
None of which is ideal in my opinion. I'd consider QueryString like pairs, XML or JSON or some other format that has parsers available out there instead if you have the option and consider storing data in a dictionary instance instead of creating an object for every expression.
Upvotes: 2