修改VSPaste from Visual Studio背景颜色
第一次是从这里听说这个插件的,亲自尝试了一下,代码高亮果然显示完全和Visual Studio相同,所以后来一直用的它(可以从这里下载),但是它有个小缺点:粘贴的时候同时也会生成和vs相同的背景颜色(background),由于我的vs使用的是深色主题,背景颜色是黑色,但是博客背景颜色是白色,所以代码最终会显示成类似这个样子:
foreach (String s in exceptionTree) { String[] x = s.Split('-'); Console.WriteLine(new String(' ', 3 * (x.Length - 1)) + x[x.Length - 1]); }
非常不和谐,后来抱着试一试的心态搜到了2篇比较有价值的文章(这里和这里),认真学习之后,决定自己尝试一番,主要思路是:首先用ildasm.exe导出VSPaste.dll的il代码到vspaste.il中,同时反编译类HTMLRootProcessor中的SyncColors的源代码,删除方法中生成背景颜色的那一段代码,然后重新编译生成dll(省略了n个步骤)即可。
下面是主要的几个步骤:
首先导出VSPaste.dll的il
ildasm VSPaste.dll /out=vspaste.il
然后,查找SyncColors方法并替换掉成如下il(主要是删除了关于生成background的代码):
.method private hidebysig instance void SyncColors(bool bgOnly) cil managed { // 代码大小 338 (0x152) .maxstack 3 .locals init (valuetype [mscorlib]System.Nullable`1<int32> V_0, valuetype [mscorlib]System.Nullable`1<int32> V_1, bool V_2) IL_0000: nop IL_0001: ldarg.0 IL_0002: ldfld valuetype [mscorlib]System.Nullable`1<int32> HTMLRootProcessor::background IL_0007: stloc.0 IL_0008: ldloca.s V_0 IL_000a: call instance !0 valuetype [mscorlib]System.Nullable`1<int32>::GetValueOrDefault() IL_000f: ldarg.0 IL_0010: ldfld valuetype [mscorlib]System.Nullable`1<int32> HTMLRootProcessor::nextBackground IL_0015: stloc.1 IL_0016: ldloca.s V_1 IL_0018: call instance !0 valuetype [mscorlib]System.Nullable`1<int32>::GetValueOrDefault() IL_001d: bne.un.s IL_0034 IL_001f: ldloca.s V_0 IL_0021: call instance bool valuetype [mscorlib]System.Nullable`1<int32>::get_HasValue() IL_0026: ldloca.s V_1 IL_0028: call instance bool valuetype [mscorlib]System.Nullable`1<int32>::get_HasValue() IL_002d: ceq IL_002f: ldc.i4.0 IL_0030: ceq IL_0032: br.s IL_0035 IL_0034: ldc.i4.1 IL_0035: brtrue.s IL_006d IL_0037: ldarg.0 IL_0038: ldflda valuetype [mscorlib]System.Nullable`1<int32> HTMLRootProcessor::color IL_003d: call instance !0 valuetype [mscorlib]System.Nullable`1<int32>::GetValueOrDefault() IL_0042: ldarg.0 IL_0043: ldflda valuetype [mscorlib]System.Nullable`1<int32> HTMLRootProcessor::nextColor IL_0048: call instance !0 valuetype [mscorlib]System.Nullable`1<int32>::GetValueOrDefault() IL_004d: bne.un.s IL_0067 IL_004f: ldarg.0 IL_0050: ldflda valuetype [mscorlib]System.Nullable`1<int32> HTMLRootProcessor::color IL_0055: call instance bool valuetype [mscorlib]System.Nullable`1<int32>::get_HasValue() IL_005a: ldarg.0 IL_005b: ldflda valuetype [mscorlib]System.Nullable`1<int32> HTMLRootProcessor::nextColor IL_0060: call instance bool valuetype [mscorlib]System.Nullable`1<int32>::get_HasValue() IL_0065: beq.s IL_006a IL_0067: ldarg.1 IL_0068: br.s IL_006b IL_006a: ldc.i4.1 IL_006b: br.s IL_006e IL_006d: ldc.i4.0 IL_006e: stloc.2 IL_006f: ldloc.2 IL_0070: brtrue IL_0151 IL_0075: nop IL_0076: ldarg.0 IL_0077: ldflda valuetype [mscorlib]System.Nullable`1<int32> HTMLRootProcessor::color IL_007c: call instance bool valuetype [mscorlib]System.Nullable`1<int32>::get_HasValue() IL_0081: brtrue.s IL_0093 IL_0083: ldarg.0 IL_0084: ldflda valuetype [mscorlib]System.Nullable`1<int32> HTMLRootProcessor::background IL_0089: call instance bool valuetype [mscorlib]System.Nullable`1<int32>::get_HasValue() IL_008e: ldc.i4.0 IL_008f: ceq IL_0091: br.s IL_0094 IL_0093: ldc.i4.0 IL_0094: stloc.2 IL_0095: ldloc.2 IL_0096: brtrue.s IL_00ab IL_0098: nop IL_0099: ldarg.0 IL_009a: ldfld class [mscorlib]System.IO.TextWriter HTMLRootProcessor::writer IL_009f: ldstr "</span>" IL_00a4: callvirt instance void [mscorlib]System.IO.TextWriter::Write(string) IL_00a9: nop IL_00aa: nop IL_00ab: ldarg.0 IL_00ac: ldarg.0 IL_00ad: ldfld valuetype [mscorlib]System.Nullable`1<int32> HTMLRootProcessor::nextColor IL_00b2: stfld valuetype [mscorlib]System.Nullable`1<int32> HTMLRootProcessor::color IL_00b7: ldarg.0 IL_00b8: ldarg.0 IL_00b9: ldfld valuetype [mscorlib]System.Nullable`1<int32> HTMLRootProcessor::nextBackground IL_00be: stfld valuetype [mscorlib]System.Nullable`1<int32> HTMLRootProcessor::background IL_00c3: ldarg.0 IL_00c4: ldflda valuetype [mscorlib]System.Nullable`1<int32> HTMLRootProcessor::color IL_00c9: call instance bool valuetype [mscorlib]System.Nullable`1<int32>::get_HasValue() IL_00ce: brtrue.s IL_00e0 IL_00d0: ldarg.0 IL_00d1: ldflda valuetype [mscorlib]System.Nullable`1<int32> HTMLRootProcessor::background IL_00d6: call instance bool valuetype [mscorlib]System.Nullable`1<int32>::get_HasValue() IL_00db: ldc.i4.0 IL_00dc: ceq IL_00de: br.s IL_00e1 IL_00e0: ldc.i4.0 IL_00e1: stloc.2 IL_00e2: ldloc.2 IL_00e3: brtrue.s IL_0150 IL_00e5: nop IL_00e6: ldarg.0 IL_00e7: ldfld class [mscorlib]System.IO.TextWriter HTMLRootProcessor::writer IL_00ec: ldstr "<span style=\"" IL_00f1: callvirt instance void [mscorlib]System.IO.TextWriter::Write(string) IL_00f6: nop IL_00f7: ldarg.0 IL_00f8: ldflda valuetype [mscorlib]System.Nullable`1<int32> HTMLRootProcessor::color IL_00fd: call instance bool valuetype [mscorlib]System.Nullable`1<int32>::get_HasValue() IL_0102: ldc.i4.0 IL_0103: ceq IL_0105: stloc.2 IL_0106: ldloc.2 IL_0107: brtrue.s IL_013e IL_0109: nop IL_010a: ldarg.0 IL_010b: ldfld class [mscorlib]System.IO.TextWriter HTMLRootProcessor::writer IL_0110: ldstr "color:" IL_0115: callvirt instance void [mscorlib]System.IO.TextWriter::Write(string) IL_011a: nop IL_011b: ldarg.0 IL_011c: ldfld class [mscorlib]System.IO.TextWriter HTMLRootProcessor::writer IL_0121: ldarg.0 IL_0122: ldfld class ColorProcessor HTMLRootProcessor::colors IL_0127: ldarg.0 IL_0128: ldflda valuetype [mscorlib]System.Nullable`1<int32> HTMLRootProcessor::color IL_012d: call instance !0 valuetype [mscorlib]System.Nullable`1<int32>::get_Value() IL_0132: callvirt instance string ColorProcessor::CssColor(int32) IL_0137: callvirt instance void [mscorlib]System.IO.TextWriter::Write(string) IL_013c: nop IL_013d: nop IL_013e: ldarg.0 IL_013f: ldfld class [mscorlib]System.IO.TextWriter HTMLRootProcessor::writer IL_0144: ldstr "\">" IL_0149: callvirt instance void [mscorlib]System.IO.TextWriter::Write(string) IL_014e: nop IL_014f: nop IL_0150: nop IL_0151: ret }
然后使用ilasm生成dll ,命令为:ilasm vspaste.il /dll /out:VSPaste
C:\Windows\Microsoft.NET\Framework\v2.0.50727>ilasm vspaste.il /dll /out:VSPaste
.dll
Microsoft (R) .NET Framework IL Assembler. Version 2.0.50727.6387
Copyright (c) Microsoft Corporation. All rights reserved.
Assembling 'vspaste.il' to DLL --> 'VSPaste.dll'
Source file is ANSI
……
生成成功后覆盖原来的VSPaste.dll就行了!(点击这里可以直接下载该dll)
不过需要注意的是2.0版本的编译器,使用4.0版本虽然可以编译成功,但是Windows Live Writer无法识别。