Chris McCall
Chris McCall

Reputation: 10397

How can I instantiate an instance of a type using a dictionary of property names/values?

I know how to do this "manually", that is, loop through the dictionary and assign property values using the key and reflection. I seem to recall, though, a method hanging off of one of the built-in .Net classes for creating class instances out of dictionaries. Only now I can't seem to find it and Google's not helping.

Am I imagining things or is there a one-liner for this?

ex:

var obj = CantRememberWhichClass.WithDictionary(Dictionary<string, object> input);

Upvotes: 1

Views: 72

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1062865

I wonder if you mean TypeConverter.CreateInstance? This is not universally supported, but some converters report true for GetCreateInstanceSupported

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1062865

You might be thinking of PopulateObjectMembers, but the API isn't an exact match. Maybe just use reflection instead?

Upvotes: 1

Related Questions