예제 #1
0
        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);
        }
예제 #2
0
        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;
            //}
        }
예제 #3
0
파일: StrategyMgr.cs 프로젝트: wanwei/sc2
        /// <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);
            }
        }
예제 #4
0
 public static StrategyAssembly GetAssembly(string file)
 {
     return(StrategyAssembly.Create(file));
 }