public void CanUseBuilderForMemberToMember() { var builder = new EvaluatorBuilder(); builder .WithActivator(new ReflectionActivator()) .Map <Foo, Bar>(m => true) .FromExpression(m => m.Message) .ToExpression(m => m.Content); var evaluator = builder.Build(); var output = evaluator.Evaluate <Foo, Bar>(new Foo { Message = "Hello, world!" }); Assert.IsNotNull(output); Assert.IsTrue(output.Content.Equals("Hello, world!", System.StringComparison.OrdinalIgnoreCase)); }
public void CanHandleConversionMemberToMember() { var builder = new EvaluatorBuilder(); builder.WithActivator(new ReflectionActivator()) .Map <Foo, BarInt>(m => true) .FromExpression(m => m.Message) .ToExpression(m => m.MessageCode, t => { t.AddConverter(new StringToIntConverter()); }); var evaluator = builder.Build(); var output = evaluator.Evaluate <Foo, BarInt>( new Foo { Message = "500" } ); Assert.IsNotNull(output); Assert.IsTrue(output.MessageCode == 500); }
public override void Interpret() { if (File.Exists(context.Request.PhysicalPathFile)) { using (FileStream fs = new FileStream(context.Request.PhysicalPathFile, FileMode.Open, FileAccess.Read, FileShare.Read)) { // calculamos el hashcode para string hashcode = Conversion.CalculateHashCode(fs); string mainClassName = "CodeRender_" + Path.GetFileNameWithoutExtension(context.Request.PhysicalPathFile); Assembly asam = cache.Get(hashcode) as Assembly; if (asam == null) { StreamReader reader = new StreamReader(fs, Encoding.Default, true, 4096); parser = new AspParser(context.Request.PhysicalPathFile, reader); parser.TagParsed += new TagParsedHandler(parser_TagParsed); parser.TextParsed += new TextParsedHandler(parser_TextParsed); parser.Error += new ParseErrorHandler(parser_Error); eb = new EvaluatorBuilder(); eb.BeginCompile("ASP"); eb.AppendNameSpace("System"); eb.AppendNameSpace("System.Web"); CodeTypeDeclaration c = eb.AppendClass(mainClassName, typeof(DinamicPage)); // eb.CreatePropertyGetForObject(c, context.App.GetType(), "App", "Context.App"); // eb.CreatePropertyGetForObject(c, context.SessionRequest == null ? typeof(Object) : context.SessionRequest.GetType(), "SessionRequest", "Context.SessionRequest"); m = eb.AppendMethod(c, "Render", MemberAttributes.Override); parser.Parse(); asam = eb.EndCompile(); // aƱadimos al cache cache.Add(hashcode, asam); } DinamicPage page = asam.CreateInstance("ASP." + mainClassName) as DinamicPage; page.Me = this; page.Context = context; page.OutStream = new StringBuilder(); context.Reply.Render = page; try { page.Render(); } catch (Exception err) { page.OutStream.AppendFormat("Error: {0}\n{1}", err.Message, err.StackTrace); } // forzar descarga de archivo if (!context.Reply.ForcedFileDownload && !context.Reply.LoadedStream) { context.Reply.LoadText(page.OutStream.ToString()); } } } else { context.Reply.Code = Codes.NOT_FOUND; } }