Skip to content

Instantly share code, notes, and snippets.

@breezhang
Created February 15, 2012 16:42
Show Gist options
  • Save breezhang/1837185 to your computer and use it in GitHub Desktop.
Save breezhang/1837185 to your computer and use it in GitHub Desktop.
find has an interface class
//System.Reflection
public class Invoker
{
private readonly List<object> _mCommand;
private const BindingFlags Flags = BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance;
public Invoker()
{
_mCommand = (from type in Assembly.GetExecutingAssembly().GetTypes()
where type.IsClass && type.GetInterfaces().Contains(typeof(IExeCute))
select Activator.CreateInstance(type)).ToList();
}
[Fact]
public static void DoTestRefTest()
{
var o = new Invoker();
o.GetCommand(CommandEnum.File).Execute();
}
private IExeCute GetCommand(CommandEnum command)
{
var single = _mCommand.Single(o => o.GetType().
GetFields(Flags)
.Any(info => (CommandEnum)info.GetValue(o) == command));
if (single == null) throw new ArgumentNullException("command");
return single as IExeCute;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment