Skip to content

Instantly share code, notes, and snippets.

@DForshner
Last active December 23, 2015 13:39
Show Gist options
  • Save DForshner/6643722 to your computer and use it in GitHub Desktop.
Save DForshner/6643722 to your computer and use it in GitHub Desktop.
Create a new instance of class by name (string) using a look up table.
// Creates new instance of a class by name (string).
public IClass GetDataSource(string className)
{
// Should handle case where the className does not exist in dictionary.
// Lookup table that maps class string names to a constructor action.
var classDictonary = new Dictionary<string, Func<IClass>>
{
{ "ClassA", () => new ClassA()},
{ "ClassB", () => new ClassB()},
{ "ClassC", () => new Class()},
};
// Create new instance
return classDictonary[reportName].Invoke();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment