Skip to content

Instantly share code, notes, and snippets.

@ezdiy
Created May 1, 2018 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ezdiy/816813e34afd48f6631a35611ff03093 to your computer and use it in GitHub Desktop.
Save ezdiy/816813e34afd48f6631a35611ff03093 to your computer and use it in GitHub Desktop.
// Destructively turn original assemblies into a mush suitable for "subclassing"
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Mono.Cecil;
using Mono.Cecil.Rocks;
class Publicize
{
static void Main(string[] args)
{
foreach (var dll in Directory.GetFiles(args[0], args[1]))
{
var ass = AssemblyDefinition.ReadAssembly(new MemoryStream(File.ReadAllBytes(dll)));
var types = ass.MainModule.GetAllTypes();
foreach (var type in types)
{
// Make all fields public
foreach (var field in type.Fields)
{
field.IsPublic = true;
field.IsInitOnly = false;
}
// Turn all methods into abstract
foreach (var method in type.Methods)
{
method.IsAbstract = true;
}
// If event name collides with fields, rename it
foreach (var vent in type.Events)
{
if (type.Fields.Any(x => x.Name == vent.Name) || type.Properties.Any(x => x.Name == vent.Name))
{
vent.Name += "Event";
}
}
// Unseal
type.IsSealed = false;
// And the type itself is to be public too
if (type.IsNested)
{
type.IsNestedPublic = true;
}
else
{
type.IsPublic = true;
}
}
Console.WriteLine(dll);
System.GC.Collect();
System.GC.Collect();
ass.Write(dll);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment