Created
May 15, 2013 22:56
-
-
Save MatthewSteeples/5588087 to your computer and use it in GitHub Desktop.
Code to rename namespaces from compiled assemblies using Mono.Cecil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Mono.Cecil; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Xml.Serialization; | |
namespace CecilTest | |
{ | |
class Program | |
{ | |
static string path = @"C:\Source\Ledgerscope\Intuit\ipp-v3-dotnet-devkit-3.0.2"; | |
static string newPath = @"C:\Source\Ledgerscope\Intuit\IL rewriting\V3"; | |
static string ipp = "Intuit.Ipp."; | |
static string version = "Intuit.Ipp.V3."; | |
static void Main(string[] args) | |
{ | |
var files = Directory.GetFiles(path, "*.dll"); | |
foreach (var item in files) | |
{ | |
AssemblyDefinition ad = AssemblyDefinition.ReadAssembly(item); | |
fixNamespace(ad, new Dictionary<string,TypeDefinition>()); | |
} | |
} | |
static void fixNamespace(AssemblyDefinition ad, Dictionary<string, TypeDefinition> references) | |
{ | |
ad.Name.Name = ad.Name.Name.Replace(ipp, version); | |
foreach (var item in ad.MainModule.AssemblyReferences) | |
{ | |
var fileName = Path.Combine(path, item.Name + ".dll"); | |
item.Name = item.Name.Replace(ipp, version); | |
if (File.Exists(fileName)) | |
{ | |
AssemblyDefinition a2 = AssemblyDefinition.ReadAssembly(fileName); | |
fixNamespace(a2, references); | |
} | |
} | |
foreach (var item in ad.MainModule.Types) | |
{ | |
if (item.Namespace.StartsWith(ipp) && !item.Namespace.StartsWith(version)) | |
{ | |
if (!references.ContainsKey(item.Name)) | |
references.Add(item.Name, item); | |
item.Namespace = item.Namespace.Replace(ipp, version); | |
} | |
} | |
//Required to update the constructor arguments | |
foreach (var item in ad.MainModule.Types) | |
{ | |
foreach (var attr in item.CustomAttributes) | |
{ | |
if (attr.HasConstructorArguments) | |
{ | |
if (attr.ConstructorArguments.Any(a => a.Type.Name == "Type")) | |
{ | |
} | |
} | |
} | |
} | |
ad.Write(Path.Combine(newPath, ad.Name.Name + ".dll")); | |
} | |
} | |
} |
Hi i am using some .dll for which i dont have source code but i want to replace some word with some another word. Is it possible ?
Can you put the Code to rename all variables, clases, methods... from compiled assemblies using Mono.Cecil ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this!