static void Main(string[] args) { //dynamic load: //load config file from args=>LoadConfig //Loader.LoadLog(LoadConfig.Log); //Loader.LoadCommunication(LoadConfig.Communication,host); //Loader.LoadThing(LoadConfig.Things,host); //very first thing to do is congfig logger //初始化 LogManager NlogProvider.Config(); Cfet2LogManager.SetLogProvider(new NlogProvider()); //inject and init the host app module var host = new Cfet2Program(); HubMaster.InjectHubToModule(host); //add communication module //var comm= new NancyCM("http://localhost:13345"); //var comm = new AspNetCoreCommunicatonModule(); //host.MyHub.TryAddCommunicationModule(comm); //todo lock comminication so no more communication can be added //add things host.AddThings(); //this is defined in a partial file of this class f12 to modify //start communication modules host.MyHub.StartCommunication(); //start all thins host.MyHub.StartThings(); //start cli loop var cli = new CliParser(host); cli.Host = host; Console.WriteLine("Cfet2 host Cli started"); while (true) { Console.Write("Cfet2> "); var command = Console.ReadLine(); try { cli.Execute(command); } catch (Exception e) { Console.WriteLine(e.Message); } if (cli.MySesstion.ShouldExit) { //quit the app break; } } }
public DynamicThingsLoader(Cfet2Program host) { //得到带分隔符的执行文件夹路径 string excuteDir = Environment.CurrentDirectory; if (!excuteDir.EndsWith(Path.DirectorySeparatorChar.ToString())) { excuteDir += Path.DirectorySeparatorChar; } //如果文件夹不存在,新建Things和dll文件夹 if (!Directory.Exists(excuteDir + "DynamicLoad" + Path.DirectorySeparatorChar + "Things")) { Directory.CreateDirectory(excuteDir + "DynamicLoad" + Path.DirectorySeparatorChar + "Things"); } if (!Directory.Exists(excuteDir + "DynamicLoad" + Path.DirectorySeparatorChar + "dll")) { Directory.CreateDirectory(excuteDir + "DynamicLoad" + Path.DirectorySeparatorChar + "dll"); } //获取代码 var coder = new Coder(); string code = coder.GetFullCode(excuteDir); //获取可用dll合集 var dller = new Dller(); string[] dlls = dller.CopyAndGetAllDlls(excuteDir); CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider(); CodeDomProvider objICodeCompiler = objCSharpCodePrivoder; CompilerParameters objCompilerParameters = new CompilerParameters(); objCompilerParameters.ReferencedAssemblies.AddRange(dlls); objCompilerParameters.GenerateExecutable = false; objCompilerParameters.GenerateInMemory = true; CompilerResults cr = objICodeCompiler.CompileAssemblyFromSource(objCompilerParameters, code); if (cr.Errors.HasErrors) { string info = "动态加载Thing编译错误:\n"; foreach (CompilerError err in cr.Errors) { info += err.ErrorText + "\n"; } throw new Exception(info); } Assembly objAssembly = cr.CompiledAssembly; object objAddThings = objAssembly.CreateInstance("DynamicAddThing"); MethodInfo objMI = objAddThings.GetType().GetMethod("AddThing"); var p = new object[1]; p[0] = host; objMI.Invoke(objAddThings, p); }