public ImportMethod ( MethodDefinition definition ) : IMemberReference | ||
definition | MethodDefinition | |
return | IMemberReference |
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); }
public void CreateAttributeWithEnumArgument() { // set up temp assembly. var assembly = Utilities.CreateTempNetAssembly(); var tableStream = assembly.NetDirectory.MetadataHeader.GetStream<TableStream>(); var assemblyTable = tableStream.GetTable<AssemblyDefinition>(); var attributeTable = tableStream.GetTable<CustomAttribute>(); var importer = new ReferenceImporter(tableStream); var assemblyDef = assemblyTable[0]; // create custom attribute. var signature = new CustomAttributeSignature(); signature.FixedArguments.Add( new CustomAttributeArgument(importer.ImportTypeSignature(typeof(DebuggableAttribute.DebuggingModes)), new ElementSignature((int)DebuggableAttribute.DebuggingModes.Default))); var attribute = new CustomAttribute(importer.ImportMethod(typeof(DebuggableAttribute).GetConstructor(new Type[] { typeof(DebuggableAttribute.DebuggingModes) })), signature); assemblyDef.CustomAttributes.Add(attribute); attributeTable.Add(attribute); // build and validate. assembly = Utilities.RebuildNetAssembly(assembly); tableStream = assembly.NetDirectory.MetadataHeader.GetStream<TableStream>(); assemblyTable = tableStream.GetTable<AssemblyDefinition>(); assemblyDef = assemblyTable[0]; attributeTable = tableStream.GetTable<CustomAttribute>(); var newAttribute = attributeTable[0]; Assert.IsTrue(assemblyDef.CustomAttributes.Contains(newAttribute)); Assert.AreEqual(attribute.Constructor.FullName, newAttribute.Constructor.FullName); Assert.AreEqual(attribute.Signature.FixedArguments.Count, newAttribute.Signature.FixedArguments.Count); for (int i = 0; i < attribute.Signature.FixedArguments.Count; i++) Utilities.ValidateArgument(attribute.Signature.FixedArguments[i], newAttribute.Signature.FixedArguments[i]); }
public void CreateAttributeWithNamedArgument() { const string fieldName = "MyField"; const string argumentValue = "MyXmlAttribute"; const string propertyName = "IsNullable"; const bool propertyValue = true; // set up temp assembly. var assembly = Utilities.CreateTempNetAssembly(); var typeSystem = assembly.NetDirectory.MetadataHeader.TypeSystem; var tableStream = assembly.NetDirectory.MetadataHeader.GetStream<TableStream>(); var fieldTable = tableStream.GetTable<FieldDefinition>(); var attributeTable = tableStream.GetTable<CustomAttribute>(); var importer = new ReferenceImporter(tableStream); // create temp field. var field = new FieldDefinition(fieldName, FieldAttributes.Static, new FieldSignature(typeSystem.String)); fieldTable.Add(field); // create custom attribute. var signature = new CustomAttributeSignature(); signature.FixedArguments.Add( new CustomAttributeArgument(typeSystem.String, new ElementSignature(argumentValue))); signature.NamedArguments.Add( new CustomAttributeNamedArgument() { ArgumentMemberType = CustomAttributeArgumentMemberType.Property, ArgumentType = typeSystem.Boolean, MemberName = propertyName, Argument = new CustomAttributeArgument(typeSystem.Boolean, new ElementSignature(propertyValue)) }); var attribute = new CustomAttribute(importer.ImportMethod(typeof(XmlAttributeAttribute).GetConstructor(new Type[] { typeof(string) })), signature); field.CustomAttributes.Add(attribute); attributeTable.Add(attribute); // build and validate. assembly = Utilities.RebuildNetAssembly(assembly); fieldTable = tableStream.GetTable<FieldDefinition>(); field = fieldTable.First(x => x.Name == fieldName); attributeTable = tableStream.GetTable<CustomAttribute>(); var newAttribute = attributeTable[0]; Assert.IsTrue(field.CustomAttributes.Contains(newAttribute)); Assert.AreEqual(attribute.Constructor.FullName, newAttribute.Constructor.FullName); Assert.AreEqual(attribute.Signature.FixedArguments.Count, newAttribute.Signature.FixedArguments.Count); for (int i = 0; i < attribute.Signature.FixedArguments.Count; i++) { Utilities.ValidateArgument(attribute.Signature.FixedArguments[i], newAttribute.Signature.FixedArguments[i]); } Assert.AreEqual(attribute.Signature.NamedArguments.Count, newAttribute.Signature.NamedArguments.Count); for (int i = 0; i < attribute.Signature.NamedArguments.Count; i++) { Utilities.ValidateNamedArgument(attribute.Signature.NamedArguments[i], newAttribute.Signature.NamedArguments[i]); } }
public void CreateSimpleAttribute() { // set up temp assembly. var assembly = Utilities.CreateTempNetAssembly(); var typeSystem = assembly.NetDirectory.MetadataHeader.TypeSystem; var tableStream = assembly.NetDirectory.MetadataHeader.GetStream<TableStream>(); var fieldTable = tableStream.GetTable<FieldDefinition>(); var attributeTable = tableStream.GetTable<CustomAttribute>(); var importer = new ReferenceImporter(tableStream); // create field. var field = new FieldDefinition("MyField", FieldAttributes.Public | FieldAttributes.Static, new FieldSignature(typeSystem.String)); fieldTable.Add(field); // create custom attribute. var signature = new CustomAttributeSignature(); signature.FixedArguments.Add(new CustomAttributeArgument(typeSystem.String, new ElementSignature("Lorem ipsum dolor sit amet."))); signature.FixedArguments.Add(new CustomAttributeArgument(typeSystem.Boolean, new ElementSignature(true))); var attribute = new CustomAttribute(importer.ImportMethod(typeof(ObsoleteAttribute).GetConstructor(new Type[] { typeof(string), typeof(bool) })), signature); field.CustomAttributes.Add(attribute); attributeTable.Add(attribute); // build and validate. assembly = Utilities.RebuildNetAssembly(assembly); tableStream = assembly.NetDirectory.MetadataHeader.GetStream<TableStream>(); fieldTable = tableStream.GetTable<FieldDefinition>(); field = fieldTable[0]; attributeTable = tableStream.GetTable<CustomAttribute>(); var newAttribute = attributeTable[0]; Assert.IsTrue(field.CustomAttributes.Contains(newAttribute)); Assert.AreEqual(attribute.Constructor.FullName, newAttribute.Constructor.FullName); Assert.AreEqual(attribute.Signature.FixedArguments.Count, newAttribute.Signature.FixedArguments.Count); for (int i = 0; i < attribute.Signature.FixedArguments.Count; i++) Assert.AreEqual(attribute.Signature.FixedArguments[i].Elements[0].Value, newAttribute.Signature.FixedArguments[i].Elements[0].Value); }
public void ImportUsingReflection() { // 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); // import members. var originalWriteLine = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }); var newWriteLine = importer.ImportMethod(originalWriteLine); Utilities.ValidateMethod(originalWriteLine, newWriteLine); var originalReadKey = typeof(Console).GetMethod("ReadKey", Type.EmptyTypes); var newReadKey = importer.ImportMethod(originalReadKey); Utilities.ValidateMethod(originalReadKey, newReadKey); // write code. var body = methodTable[0].MethodBody; body.Instructions.Clear(); body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Ldc_I4, 1337)); body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Call, newWriteLine)); body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Call, newReadKey)); 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 writeLineReference = methodTable[methodTable.Count - 1].MethodBody.Instructions[1].Operand; Assert.IsInstanceOfType(writeLineReference, typeof(MemberReference)); Utilities.ValidateMethod(originalWriteLine, (MemberReference)writeLineReference); var readKeyReference = methodTable[methodTable.Count - 1].MethodBody.Instructions[2].Operand; Assert.IsInstanceOfType(readKeyReference, typeof(MemberReference)); Utilities.ValidateMethod(originalReadKey, (MemberReference)readKeyReference); }