public IList <IStrategyAssembly> Scan(String path) { if (!Directory.Exists(path)) { return(new List <IStrategyAssembly>()); } String[] files = Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories); List <IStrategyAssembly> plugins = new List <IStrategyAssembly>(); for (int i = 0; i < files.Length; i++) { string file = files[i]; if (IsIgnoreDll(file)) { continue; } StrategyAssembly strategyAssembly = GetAssembly(file); if (strategyAssembly != null && strategyAssembly.GetAllStrategies().Count > 0) { plugins.Add(strategyAssembly); } } return(plugins); }
internal static StrategyAssembly Create(string path) { //try //{ StrategyAssembly strategyAssembly = new StrategyAssembly(); byte[] buffer = System.IO.File.ReadAllBytes(path); Assembly ass = Assembly.Load(buffer); //Assembly ass = Assembly.LoadFrom(path); if (ass == null) { return(null); } strategyAssembly.fullPath = path; strategyAssembly.assemblyName = ass.GetName().Name; Type[] types = ass.GetTypes(); for (int i = 0; i < types.Length; i++) { Type classType = types[i]; //类上定义了特性才行 Type attributeType = typeof(StrategyAttribute); if (!classType.IsDefined(attributeType, false)) { continue; } Type inheritType = classType.GetInterface(typeof(IStrategy).FullName); if (inheritType == null) { continue; } var attributes = classType.GetCustomAttributes(); foreach (var attribute in attributes) { if (attribute.GetType() == attributeType) { string id = (String)attribute.GetType().GetProperty("ID").GetValue(attribute); String name = (String)attribute.GetType().GetProperty("Name").GetValue(attribute); string desc = (String)attribute.GetType().GetProperty("Desc").GetValue(attribute); string spath = (String)attribute.GetType().GetProperty("Path").GetValue(attribute); StrategyInfo strategyInfo = new StrategyInfo(strategyAssembly, classType, id, name, desc, spath); strategyAssembly.AddStrategyInfo(strategyInfo); } } } strategyAssembly.InitPaths(); return(strategyAssembly); //} //catch (Exception e) //{ // //Console.WriteLine(e.Data); // return null; //} }
/// <summary> /// 刷新一个assembly /// </summary> /// <param name="ass"></param> public void Refresh(IStrategyAssembly ass) { int index = this.assemblies.IndexOf(ass); this.assemblies.Remove(ass); string path = ass.FullPath; StrategyAssembly newAss = StrategyAssembly.Create(path); if (newAss != null) { this.assemblies.Insert(index, newAss); } }
public static StrategyAssembly GetAssembly(string file) { return(StrategyAssembly.Create(file)); }