Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save atheken/5cad91c1f26f49a225108d39ed60b25f to your computer and use it in GitHub Desktop.
Save atheken/5cad91c1f26f49a225108d39ed60b25f to your computer and use it in GitHub Desktop.
Code to rename namespaces from compiled assemblies using Mono.Cecil
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"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment