/// <summary> /// 生成代码 /// </summary> /// <param name="environment">文件写入器</param> public IGenerateAsyncResult Generate(IEnvironment environment) { lock (syncRoot) { if (generating != null) { return(generating); } generating = new GenerateAsyncResult(); Environment = environment; } ThreadPool.QueueUserWorkItem(BeginGenerate); return(generating); }
/// <summary> /// 开始生成代码 /// </summary> private void BeginGenerate(object state) { var isExceotion = false; try { Environment.Begin(); GenerateTypes(GetGeneraterTypes()); } catch (Exception ex) { isExceotion = true; Environment.Rollback(); if (OnException != null) { OnException.Invoke(ex); } else { throw ex; } } finally { try { if (!isExceotion) { Environment.Commit(); if (OnCompleted != null) { OnCompleted.Invoke(); } } } finally { generating.IsCompleted = true; Environment = null; generating = null; } } }