public IEnumerable <Edge> Apply(Vertex source) { foreach (var kvp_iter in Logic(source.Object)) { var kvp = kvp_iter; // just make resharper happeh Func <Vertex> createVertex = () => new Vertex(kvp.Value, ++_idFactory, source.DistanceFromRoot + 1); if (kvp.Value == null) { yield return(new Edge(kvp.Key, source, createVertex())); } else { var contains = SafetyTools.SafeEval(() => _cache.ContainsKey(kvp.Value), true); if (!contains) { SafetyTools.SafeDo(() => _cache.Add(kvp.Value, createVertex())); } var vertex = SafetyTools.SafeEval(() => _cache[kvp.Value], createVertex); yield return(new Edge(kvp.Key, source, vertex)); } } }
internal CodegenUnit(AssemblyName asmName) { var fileName = asmName.Name + ".dll"; var pdbName = asmName + ".pdb"; // so that we can run multiple tests at once and not lose the info if (UnitTest.CurrentTest != null) { fileName = asmName + ", " + UnitTest.PersistentId + ".dll"; pdbName = asmName + ".pdb"; } #if TRACE try { _asm = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.RunAndSave); _mod = _asm.DefineDynamicModule(fileName, true); // Mark generated code as debuggable. // See http://blogs.msdn.com/jmstall/archive/2005/02/03/366429.aspx for explanation. var daCtor = typeof(DebuggableAttribute).GetConstructor(new [] { typeof(DebuggableAttribute.DebuggingModes) }); var daBuilder = new CustomAttributeBuilder(daCtor, new object[] { DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.Default }); _asm.SetCustomAttribute(daBuilder); // Mark generated code as non-user code. // See http://stackoverflow.com/questions/1423733/how-to-tell-if-a-net-assembly-is-dynamic for explanation. var cgCtor = typeof(CompilerGeneratedAttribute).GetConstructor(Type.EmptyTypes); var cgBuilder = new CustomAttributeBuilder(cgCtor, new object [] {}); _asm.SetCustomAttribute(cgBuilder); var hasAlreadyBeenDumped = false; Action dumpAssembly = () => { if (!hasAlreadyBeenDumped && _asm != null) { try { // todo. before dumping make sure that all types are completed or else the dump will simply crash // this is a complex task, but it needs to be resolved for generic case _asm.Save(fileName); } catch (Exception ex) { var trace = String.Format("Codegen unit '{0}' has failed to dump the asm:{1}{2}", asmName.FullName, Environment.NewLine, ex); Log.WriteLine(trace); SafetyTools.SafeDo(() => { File.WriteAllText(fileName, trace); File.Delete(pdbName); }); } finally { hasAlreadyBeenDumped = true; } } }; _dumpAssembly = dumpAssembly; // do not use DomainUnload here because it never gets fired for default domain // however, we need not to neglect because R#'s unit-test runner never exits process AppDomain.CurrentDomain.DomainUnload += (o, e) => dumpAssembly(); AppDomain.CurrentDomain.ProcessExit += (o, e) => dumpAssembly(); AppDomain.CurrentDomain.UnhandledException += (o, e) => dumpAssembly(); } catch (Exception ex) { var trace = String.Format("Codegen unit '{0}' has failed to initialize:{1}{2}", asmName.FullName, Environment.NewLine, ex); Log.WriteLine(trace); SafetyTools.SafeDo(() => { File.WriteAllText(fileName, trace); File.Delete(pdbName); }); throw; } #else _asm = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Run); _asm.SetCustomAttribute(new CustomAttributeBuilder(typeof(CompilerGeneratedAttribute).GetConstructor(Type.EmptyTypes), new object[] { })); _mod = _asm.DefineDynamicModule(fileName, false); #endif }