Created
February 15, 2012 16:42
-
-
Save breezhang/1837185 to your computer and use it in GitHub Desktop.
find has an interface class
This file contains 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
//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