这些天,正在学习.Net的框架。
找到一个较大的工程,反编译来学习。
这个框架用到了当前所有能想到的东西,如prism,Infragistics公司的插件,如RIBBON,Layoutmgr,是基于WPF的。
其中,想了解一下LAYOUT切换的工过程,所以分析一下XAML。
所以用到了ILSpy。
从sourceforge下载了ILSpy的程序和源码。
但发现一个问题,当我们另存为时,得到的baml文件。很不方便。
所以,打算自己改改ILSpy程序。
研究了一会,找到一个地方:
public class ResourceEntryNode : ILSpyTreeNode
{
public override bool Save(DecompilerTextView textView)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.FileName = Path.GetFileName(DecompilerTextView.CleanUpName(key));
if (dlg.ShowDialog() == true) {
data.Position = 0;
using (var fs = dlg.OpenFile()) {
data.CopyTo(fs);
}
}
return true;
}
}
很快找到它的继承类:
添加了如下代码:
using System.Xml;
using System.Xml.Linq;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.ILSpy;
using ICSharpCode.ILSpy.TextView;
using ICSharpCode.ILSpy.TreeNodes;
using Mono.Cecil;
using Ricciolo.StylesExplorer.MarkupReflection;
using Microsoft.Win32;
using System.Text;
namespace ILSpy.BamlDecompiler
{
public sealed class BamlResourceEntryNode : ResourceEntryNode
{
string curstr = "";
public override bool Save(DecompilerTextView textView)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.FileName = Path.GetFileName(DecompilerTextView.CleanUpName(base.key));
dlg.FileName=dlg.FileName.Replace(".baml", ".xaml");
if (dlg.ShowDialog() == true)
{
string strCurFileName = dlg.FileName;
FileStream fs=null;
//定义文件信息对象
FileInfo finfo = new FileInfo(strCurFileName);
//判断文件是否存在以及是否大于
if (finfo.Exists)
{
//删除该文件
finfo.Delete();
}
//创建只写文件流
fs = finfo.OpenWrite();
//根据上面创建的文件流创建写数据流
using (StreamWriter w=new StreamWriter(fs, Encoding.Default))
{
//设置写数据流的起始位置为文件流的末尾
w.BaseStream.Seek(0, SeekOrigin.Begin);
w.Write(curstr);
w.Flush();
w.Close();
}
}
return true;
}
}
忘了这段代码:
bool LoadBaml(AvalonEditTextOutput output)
{
var asm = this.Ancestors().OfType<AssemblyTreeNode>().FirstOrDefault().LoadedAssembly;
Data.Position = 0;
XDocument xamlDocument = LoadIntoDocument(asm.GetAssemblyResolver(), asm.AssemblyDefinition, Data);
curstr = xamlDocument.ToString();
output.Write(curstr);
return true;
}
搞定
代码:http://download.csdn.net/detail/haoyujie/5240897
可执行程序(懒人们有福了): http://download.csdn.net/detail/haoyujie/5240882