using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Reflection; namespace Mesnac.Maths { public class Factory { #region 单例模式 private static Factory _this; public static Factory Instance { get { if (null == _this) _this = new Factory(); return _this; } } private Factory() { this.AllMaths = new Dictionary(); GetMaths(); } #endregion #region 属性 public Dictionary AllMaths { get; set; } #endregion public string GetMethodName(string method) { string ss = method.Replace(" ", ""); if (ss.IndexOf("(") > 0) { return ss.Substring(0, ss.IndexOf("(")); } return ss; } public string GetMethodParameter(string method) { string ss = method.Replace(" ", ""); if (ss.IndexOf("(") >= 0) { ss = ss.Substring(ss.IndexOf("(") + 1); } else { return string.Empty; } if (ss.IndexOf(")") >= 0) { return ss.Substring(0, ss.IndexOf(")")); } return ss; } public string[] GetParameters(string parameters) { List Result = new List(); string ss = parameters; while (true) { string parameter = string.Empty; if (ss.StartsWith("{")) { parameter = ss.Substring(0, ss.IndexOf("}") + 1).Trim(); ss = ss.Substring(ss.IndexOf("}") + 1).Trim(); } else if (ss.StartsWith("[")) { parameter = ss.Substring(0, ss.IndexOf("]") + 1).Trim(); ss = ss.Substring(ss.IndexOf("]") + 1).Trim(); } else if (ss.StartsWith("(")) { parameter = ss.Substring(0, ss.IndexOf(")") + 1).Trim(); ss = ss.Substring(ss.IndexOf(")") + 1).Trim(); } else if (ss.StartsWith("<")) { parameter = ss.Substring(0, ss.IndexOf(">") + 1).Trim(); ss = ss.Substring(ss.IndexOf(">") + 1).Trim(); } else if (ss.IndexOf(",") > 0) { parameter = ss.Substring(0, ss.IndexOf(",")).Trim(); ss = ss.Substring(ss.IndexOf(",") + 1).Trim(); } else { parameter = ss.Replace(")", "").Replace(",", ""); ss = string.Empty; } if ((string.IsNullOrWhiteSpace(ss)) && (string.IsNullOrWhiteSpace(parameter))) { break; } if (string.IsNullOrWhiteSpace(parameter)) { continue; } Result.Add(parameter); } return Result.ToArray(); } public object Execute(string method, object[] buff) { string name = GetMethodName(method); if (string.IsNullOrWhiteSpace(name)) { if (buff.Length == 1) { return buff[0]; } else { return buff; } } string ps = GetMethodParameter(method); IMath math = null; this.AllMaths.TryGetValue(name, out math); if (math != null) { return math.Execute(GetParameters(ps), buff); } if (buff.Length > 0) { if (buff.Length == 1) { return buff[0]; } else { return buff; } } return null; } private void GetMaths(FileInfo fi) { if (fi.Extension.ToLower() != ".dll") { return; } Assembly ass = Assembly.LoadFile(fi.FullName); if (ass == null) { return; } Type[] types = ass.GetTypes(); foreach (Type t in types) { Type a = t.GetInterface(typeof(IMath).FullName); if (a != null) { IMath m = (IMath)Activator.CreateInstance(t); this.AllMaths.Add(m.Name, m); } } } private void GetMaths() { string path = this.GetType().Assembly.Location; //DirectoryInfo dir = new DirectoryInfo((new FileInfo(path)).Directory + "\\Mesnac.Maths\\"); DirectoryInfo dir = new DirectoryInfo((new FileInfo(path)).Directory + "\\Data\\DeviceConfig\\Mesnac.Maths\\"); FileInfo[] files = dir.GetFiles(); foreach (FileInfo fi in files) { try { GetMaths(fi); } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(ex.Message); ICSharpCode.Core.LoggingService.Error(ex.StackTrace); } } } } }