public void AddStringReference()
{
const string testConstant = "Lorem ipsum.";
// set up temp assembly.
var assembly = Utilities.CreateTempNetAssembly();
var tableStream = assembly.NetDirectory.MetadataHeader.GetStream<TableStream>();
var methodTable = tableStream.GetTable<MethodDefinition>();
var importer = new ReferenceImporter(tableStream);
// write code.
var body = methodTable[0].MethodBody;
body.Instructions.Clear();
body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Ldstr, testConstant));
body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Call,
importer.ImportMethod(typeof(Console).GetMethod("WriteLine", new Type[]
{
typeof(string)
}))));
body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Call,
importer.ImportMethod(typeof(Console).GetMethod("ReadKey", Type.EmptyTypes))));
body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Pop));
body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Ret));
// build and validate.
assembly = Utilities.RebuildNetAssembly(assembly);
methodTable = assembly.NetDirectory.MetadataHeader.GetStream<TableStream>().GetTable<MethodDefinition>();
var operand = methodTable[0].MethodBody.Instructions[0].Operand;
Assert.IsInstanceOfType(operand, typeof(string));
Assert.AreEqual(testConstant, operand);
}