Made a few functions that allow you to instantiate any object at runtime when you only know it’s type.

I know the title is weird, but bear with me. Basically this lets you get a populated instance of any object without having to know anything about it other then it's type. Simply pass the object type to the InstantiateObject function and it will instantiate it, and all necessary arguments recursively. You can also pass in a true to get it to add an object to any collection it encounters along the way (assuming the collection implements an "Add" method).

As an example: I'm using this in a project to generate XML snippets for data service contracts by simply going through a lib, passing all types to this function, and serializing it's output with DataContractSerializer.

You can look at it here:

I'll greatly appreciate any critique and advice!

by ultra_sabreman via /r/csharp

Leave a Reply