Created
January 18, 2014 20:56
-
-
Save Deathspike/8496311 to your computer and use it in GitHub Desktop.
simple reflection command with interface C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq; | |
| using System.Reflection; | |
| namespace reflectos { | |
| // interface to implement a command | |
| interface ICommand { | |
| string Name { get; } | |
| void Run(); | |
| } | |
| // los examplos | |
| class Example : ICommand { | |
| public string Name { get { return "example"; } } | |
| public void Run() { | |
| Console.WriteLine("epicness"); | |
| } | |
| } | |
| // los loados example | |
| class Program { | |
| static void Main(string[] args) { | |
| var thisAssembly = Assembly.GetExecutingAssembly(); | |
| var commands = thisAssembly.GetTypes() | |
| .Where(x => x.GetInterface(typeof(ICommand).FullName) != null) | |
| .Select(x=>Activator.CreateInstance(x) as ICommand); | |
| var byName = commands.ToDictionary(x=>x.Name, x=>x); | |
| byName["example"].Run(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment