OK, I hope this is a complete testcase. Please let me know if anything
is missing:
Program.cs:
===========
using System;
using System.ComponentModel;
public class BlahTypeConverter : StringConverter
{
public override bool
GetStandardValuesSupported(ITypeDescriptorContext context)
{
Console.WriteLine("Inside GetStandardValuesSupported");
return true;
}
}
static class Program
{
static void Main()
{
new BlahTypeConverter().GetStandardValuesSupported(null);
}
}
TestCase.cs:
===========
using System.Diagnostics;
using System.Linq;
using Mono.Cecil;
static partial class Program
{
static void Main()
{
var path = @"Program.exe";
var asm = AssemblyDefinition.ReadAssembly(path);
var type = asm.MainModule.Types.Single(t => t.Name ==
"BlahTypeConverter");
var virtMeth = type.Methods.Single(m => m.Name ==
"GetStandardValuesSupported" && m.Parameters.Count > 0);
var baseType = type.BaseType.Resolve();
baseType = baseType.BaseType.Resolve();
var baseMeth = baseType.Methods.Single(m => m.Name ==
"GetStandardValuesSupported" && m.Parameters.Count > 0);
var imported = asm.MainModule.Import(baseMeth);
virtMeth.Overrides.Add(imported);
var outputPath = @"Program.modified.exe";
asm.Write(outputPath);
var process = Process.Start(new ProcessStartInfo
{
FileName = @"C:\Program Files\Microsoft SDKs\Windows\v7.0A
\bin\NETFX 4.0 Tools\PEVerify.exe",
Arguments = outputPath,
UseShellExecute = false
});
process.WaitForExit();
}
}
Steps to reproduce:
• Compile Program.cs into Program.exe targeting .NET version 1.1, for
example using this command line:
mcs Program.cs
• Compile TestCase.cs normally and run it
• Observe PEverify error:
[MD]: Error: MethodImpl's Decl (token=0x0a00000e)
and Body (token=0x06000007) method signatures
do not match. [token:0x00000001]
• Try running Program.modified.exe and observe exception:
Unhandled Exception: System.IO.FileNotFoundException:
Could not load file or assembly 'System, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' or one
of its dependencies. The system cannot find the file specified.
• Now compile Program.cs targeting .NET 2.0, for example using:
gmcs Program.cs
• Run TestCase.exe on that one
• Observe that it works
--
--
mono-cecil