Skip to content

Instantly share code, notes, and snippets.

@Deathspike
Created January 18, 2014 20:56
Show Gist options
  • Select an option

  • Save Deathspike/8496311 to your computer and use it in GitHub Desktop.

Select an option

Save Deathspike/8496311 to your computer and use it in GitHub Desktop.
simple reflection command with interface C#
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