[LinqPad妙用]-在Net MVC中反射调用LinqPad中的Dump函数
LinqPad有个非常强大的Dump函数。这篇讲解一下如何将Dump函数应用在.Net MVC Web开发中。
先看效果:
一、用.Net Reflector反编译LinqPad.exe,找出Dump函数的定义:
经过反编译发现,Dump函数调用了LINQPad.ObjectGraph.Formatters.XhtmlWriter类中FormatObject函数,把对象转成了Html。

二、反射调用FormatObject函数:
由于FormatObject函数是protect类型,不能直接调用,只能反射了。

三、在.Net MVC中使用Dump:
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 = )
{
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 = )
{
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; }
}
}
[LinqPad妙用]-在Net MVC中反射调用LinqPad中的Dump函数的更多相关文章
- 在C++中反射调用.NET(二)
反射调用返回复杂对象的.NET方法 定义数据接口 上一篇在C++中反射调用.NET(一)中,我们简单的介绍了如何使用C++/CLI并且初步使用了反射调用.NET程序集的简单方法,今天我们看看如何在C+ ...
- 在C++中反射调用.NET(一)
为什么要在C++中调用.NET 一般情况下,我们常常会在.NET程序中调用C/C++的程序,使用P/Invoke方式进行调用,在编写代码代码的时候,首先要导入DLL文件,然后在根据C/C++的头文件编 ...
- 在C++中反射调用.NET(三)
在.NET与C++之间传输集合数据 上一篇<在C++中反射调用.NET(二)>中,我们尝试了反射调用一个返回DTO对象的.NET方法,今天来看看如何在.NET与C++之间传输集合数据. 使 ...
- 如何在c语言中源文件调用另一个源文件的函数
在源文件A1.c中调用A2.c 中的函数有两种方法: 1.在A2.c中有完整的函数定义,在A1.c中添加一下要用到的函数原型(声明)就可以了,例如:在A2.c中:有函数void A2(){...};在 ...
- Java_通过反射调用类中的方法
先上一个基本的封装: /** * 获取classType * * @param type * @param provinceCode * @param cityCode * @return * @th ...
- 反射调用android系统级API函数
try { Class<?> mClass = Class.forName("com.android.server.wifi.WifiSettingsStore"); ...
- 在VSTO界面中,调用xll中的函数
最近研究各种有点迷茫了,原来Xll的加载宏直接可以在C#中调用的,我又各种Out了. 先说明一下,在VBA中,如何调用吧 XLLFound = Application.RegisterXLL(This ...
- Java SpringMvc+hibernate架构中,调用Oracle中的sp,传递数组参数
一.问题 我们调用数据,大都是可以直接获取表中的数据,或者用复杂点的sql语句组成的.但是,有时候,当这样达不到我们要的全部数据的时候,这时,我们就用到了存储过程[sp],如果sp需要参数是数组的话, ...
- Xamarin Android Webview中JS调用App中的C#方法
参考链接:https://github.com/xamarin/recipes/tree/master/Recipes/android/controls/webview/call_csharp_fro ...
随机推荐
- ionic ion-list 滑到底部自动加载数据案例
<ion-content> <ion-list> <ion-item ng-repeat="item in items track by $index" ...
- weblogic使用脚本部署
--本机 (/common/bin/wlst.sh (2)connect('weblogic','weblogic1','t3://localhost:7001') (3)progress=deplo ...
- uva 10026 Problem C: Edit Step Ladders
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- keep out layer PK board shape
在进行设计pcb时,注意:板边线只能用PLACE LINE画线条,不能画具有电气性能的导线关于边界设置有三种,一.在 keepout layer 定义电气边界.二.design->board s ...
- 为ownCloud配置SSL连接
为ownCloud配置SSL连接 在你开始使用ownCloud之前,强烈建议你在ownCloud中启用SSL支持.使用SSL可以提供重要的安全好处,比如加密ownCloud流量并提供适当的验证.在本教 ...
- css属性之transition
浏览器支持 Internet Explorer 10.Firefox.Opera 和 Chrome 支持 transition 属性. Safari 支持替代的 -webkit-transition ...
- Android 按钮按下效果
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="htt ...
- HDOJ 1248
完全背包. 模版. 物品的价值等价于体积. #include <stdio.h> #include <string.h> using namespace std; int ma ...
- TypeScript 素描 - 装饰器
/* 装饰器 简单理解为C#中的Attribute 可以装饰到类.函数.讯问符.属性.参数上 语法 @xxx 装饰器其实是一个函数 @xxx 就要有一个 function xxx 多个装饰器可以用来装 ...
- iOS 压缩与裁剪图片问题
我们假设要在截图中的举行图片展示区显示图片,由于原图片的宽高比例与图片显示窗口的宽高比例不一定相同,所以,直接将图片扔进去会改变图片的宽高比例,展示效果不好. 这时你可能想到设置UIImageView ...
