使用Reflector.FileDisassembler反编译DLL

本文来源自:http://www.cnblogs.com/wuliangbo/archive/2009/02/04/FileDisassembler.html

Reflector for .NET 下载地址:http://www.aisto.com/roeder/dotnet/

Reflector.FileDisassembler.dll cs文件的工具下载地址 : http://www.denisbauer.com/Downloads/Reflector.FileDisassembler.zip

1.下载Reflector.FileDisassembler.zip,解压后得到一些文件,其中Reflector.FileDisassembler.dll是已经编译好的,其它是它源码;

2.打开Reflector,在view菜单下的Add-Ins,将Reflector.FileDisassembler.dll添加到里面;

3.open一个dll,exe,或mcl为后缀的文件,当然要支持.net的

4.选择要反编译的dll(会在列表中显示),选择Tools->File Disassembler,在右窗口会出现设置存放源码位置的路径,点击产生就可以了!

但是使用FileDisassembler编译成CS文件会有一个Bug, 就是变量、类和名称空间有时会重命名。

幸好FileDisassembler发布的时候带有源代码,也就省得去Reflect一把了,毕竟机器生成的临时变量名看起来还是很不爽的。修复这个bug很简单,修改了一下CreateFile方法,同时添加了一个GetUniqueFileName方法,源代码如下:

private StreamWriter CreateFile(string directory, string fileName)  
{  
    directory = directory.Replace("<", "_");  
    directory = directory.Replace(">", "_");  
    directory = directory.Replace(":", "_");  
    directory = directory.Replace("|", "_");  
    directory = directory.Replace("?", "_");  
    directory = directory.Replace("*", "_");  
    directory = Path.Combine(this.outputDirectory, directory);  
  
    ILanguage language = this.LanguageManager.ActiveLanguage;  
    string fileExtension = language.FileExtension;  
  
    fileName = fileName.Replace("<", "_");  
    fileName = fileName.Replace(">", "_");  
    fileName = fileName.Replace(":", "_");  
    fileName = fileName.Replace("|", "_");  
    fileName = fileName.Replace("?", "_");  
    fileName = fileName.Replace("*", "_");  
    fileName = Path.Combine(directory, fileName);  
    fileName = Path.ChangeExtension(fileName, fileExtension);  
  
    fileName = GetUniqueFileName(fileName);  
    this.WriteLine(fileName);  
  
    directory = fileName.Substring(0, fileName.LastIndexOf('""'));  
    if (!Directory.Exists(directory))  
    {  
        Directory.CreateDirectory(directory);  
    }  
  
    StreamWriter writer = new StreamWriter(fileName);  
    return writer;  
}  
  
private Hashtable fileNames = new Hashtable();  
  
private string GetUniqueFileName(string fileName)  
{  
    string strExtension = Path.GetExtension(fileName);  
    fileName = fileName.Replace(strExtension, "");  
  
    string _fileName = fileName.ToLower();  
    bool bFirstRename = true;  
    while(fileNames.Contains(_fileName))  
    {  
        string existFileName = (string)fileNames[_fileName];  
        string [] efnParts = existFileName.Split(new char[] { '""' });  
        string [] fnParts = fileName.Split(new char[] { '""' });  
        for (int i = 0; i < fnParts.Length; ++i)  
        {  
            if (efnParts[i] != fnParts[i])  
            {  
                if (bFirstRename)  
                {  
                    fnParts[i] += '_';  
                    bFirstRename = false;  
                }  
                fnParts[i] += 'x';  
                break;  
            }  
        }  
        fileName = string.Empty;  
        for (int i = 0; i < fnParts.Length; ++i)  
        {  
            fileName += '""' + fnParts[i];  
        }  
        fileName = fileName.Substring(1);  
        _fileName = fileName.ToLower();  
    }  
    fileNames[_fileName] = fileName;  
    return fileName + strExtension;  
} 


你可能感兴趣的:(C#,filenames,string,dll,.net,tools,exe)