<p>I have an Interface called <code>IStep</code> that can do some computation (See "<a href="http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html" rel="nofollow noreferrer">Execution in the Kingdom of Nouns</a>"). At runtime, I want to select the appropriate implementation by class name.</p>

<pre>
// use like this:
IStep step = GetStep(sName);
</pre>

[{'answer_id': 19658, 'author': 'Ian Nelson', 'author_id': 2084, 'author_profile': 'https://Stackoverflow.com/users/2084', 'pm_score': 2, 'selected': False, 'text': '<p>If the implementation has a parameterless constructor, you can do this using the System.Activator class. You will need to specify the assembly name in addition to the class name:</p>\n\n<pre><code>IStep step = System.Activator.CreateInstance(sAssemblyName, sClassName).Unwrap() as IStep;\n</code></pre>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx</a></p>\n'}, {'answer_id': 19663, 'author': 'lubos hasko', 'author_id': 275, 'author_profile': 'https://Stackoverflow.com/users/275', 'pm_score': 4, 'selected': True, 'text': '<p>Your question is very confusing...</p>\n\n<p>If you want to find types that implement IStep, then do this:</p>\n\n<pre><code>foreach (Type t in Assembly.GetCallingAssembly().GetTypes())\n{\n  if (!typeof(IStep).IsAssignableFrom(t)) continue;\n  Console.WriteLine(t.FullName + " implements " + typeof(IStep).FullName);\n}\n</code></pre>\n\n<p>If you know already the name of the required type, just do this</p>\n\n<pre><code>IStep step = (IStep)Activator.CreateInstance(Type.GetType("MyNamespace.MyType"));\n</code></pre>\n'}, {'answer_id': 19667, 'author': 'Daren Thomas', 'author_id': 2260, 'author_profile': 'https://Stackoverflow.com/users/2260', 'pm_score': 1, 'selected': False, 'text': '<p>Based on what others have pointed out, this is what I ended up writing:</p>\n\n<pre>\n/// \n/// Some magic happens here: Find the correct action to take, by reflecting on types \n/// subclassed from IStep with that name.\n/// \nprivate IStep GetStep(string sName)\n{\n    Assembly assembly = Assembly.GetAssembly(typeof (IStep));\n\n    try\n    {\n        return (IStep) (from t in assembly.GetTypes()\n                        where t.Name == sName && t.GetInterface("IStep") != null\n                        select t\n                        ).First().GetConstructor(new Type[] {}\n                        ).Invoke(new object[] {});\n    }\n    catch (InvalidOperationException e)\n    {\n        throw new ArgumentException("Action not supported: " + sName, e);\n    }\n}\n</pre>\n'}, {'answer_id': 19669, 'author': 'samjudson', 'author_id': 1908, 'author_profile': 'https://Stackoverflow.com/users/1908', 'pm_score': 0, 'selected': False, 'text': '<p>Well Assembly.CreateInstance would seem to be the way to go - the only problem with this is that it needs the fully qualified name of the type, i.e. including the namespace.</p>\n'}]
['https://Stackoverflow.com/questions/19656', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2260/']