I have been struggling to change the BaseType of a TypeDefinition in specific scenario. Let's say we have below assemblies.
Assembly1:
Class MyAssembly1Class: Test1Class{}
Assembly2:
Class MyAssembly2Class: Test2Class{}
Now I want to change the Base class of "MyAssembly1Class" defined in "Assembly1" to "MyAssembly2Class" defined in assembly2. i.e.
Class MyAssembly1Class: MyAssembly2Class{}
How can this be achieved?
I tried code below:
public static void UpdateDerviedTextBoxTypes(AssemblyDefinition main, AssemblyDefinition otherAssembly)
{
TypeReference injectionTextBoxRef = null;
injectionTextBoxRef = otherAssembly.MainModule.GetType("MyAssembly2Class. Test2Class");
if (injectionTextBoxRef == null)
{
return;
}
foreach (TypeDefinition type in main.MainModule.Types)
{
if (type.IsClass && type.BaseType != null && type.BaseType.FullName == "MyAssembly1Class.Test1Class")
{
type.BaseType = injectionTextBoxRef;
}
}
}
though it does not throw any exception or error, but on loading the output dll on ildasm.exe, I observed that basetype is not changed.
Let's say the derived class that you are going to change is:
class MyTextBox: TextBox { }
Your's class
Class NewTextBox:TextBox {
}
If you open MyTextClass constructor definition in ildasm you will notice an instruction calling BaseClass constructor i.e. TextBox::.ctor() This must be replaced with NewTextBox reference.i.e. NewTextBox::.ctor()
try this:
injectionTextBoxRef: Type reference of the class we are replacing with.
textBoxRef: Type reference of the base class of the class you are replacing.
{