[LinqPad clever use]-reflection and call the Dump function in LinqPad in Net MVC, mvclinqpad

Source: Internet
Author: User

[LinqPad clever use]-reflection and call the Dump function in LinqPad in Net MVC, mvclinqpad

LinqPad has a very powerful Dump function. This article explains how to apply the Dump function to. Net MVC Web development.

First look at the effect:

  

1. Use. Net reflectorto decompile linqpad.exe to find the Dump function definition:

After decompilation, we found that the Dump function called the FormatObject function in the LINQPad. ObjectGraph. Formatters. XhtmlWriter class and converted the object into Html.

 

Ii. Call the FormatObject function through reflection:

Because the FormatObject function is of the protect type, it cannot be called directly and can only be reflected.
  

3. Use Dump in. Net MVC:

BigDump. cs

using System;using System.Collections.Generic;using System.Linq;using System.Web.Mvc;using System.Reflection;using System.IO;using System.Text;namespace System.Web{    public static class BigDump    {        private static MethodInfo dumpMethodInfo = null;        private static object xhtmlWriterInstance = null;        private static void Init()        {            if (dumpMethodInfo == null || xhtmlWriterInstance == null)            {                var asm = Assembly.LoadFile(HttpContext.Current.Server.MapPath("~/Lib/LINQPad.exe"));                Type t1 = asm.GetType("LINQPad.ObjectGraph.Formatters.XhtmlWriter");                ConstructorInfo t1Constructor = t1.GetConstructor(new Type[] { typeof(TextWriter), typeof(bool) });                xhtmlWriterInstance = t1Constructor.Invoke(new Object[] { new StringWriter(new StringBuilder()), false });                dumpMethodInfo = t1.GetMethod("FormatObject", BindingFlags.Instance | BindingFlags.NonPublic);            }        }        public static MvcHtmlString Render<T>(this T o, string description = "", int? depth = 3)        {            Init();            var result = dumpMethodInfo.Invoke(xhtmlWriterInstance, new Object[] { o, description, depth, new TimeSpan() });            return new MvcHtmlString(result as string);        }        public static List<MvcHtmlString> Stack { get; set; }        static BigDump()        {            Stack = new List<MvcHtmlString>();        }        public static T Dump<T>(this T o, string description = "", int? depth = 3)        {            Stack.Add(o.Render(description, depth));            return o;        }        public static void Clear()        {            Stack = new List<MvcHtmlString>();        }        public static MvcHtmlString Flush()        {            var html = new MvcHtmlString(string.Join("\r\n", Stack.Select(r => r.ToHtmlString()).ToArray()));            Clear();            return html;        }    }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.