Exemplo n.º 1
0
 /// <summary>
 /// Creates a new member cloner targeting the provided image.
 /// </summary>
 /// <param name="targetImage">The image to target.</param>
 /// <exception cref="ArgumentNullException">Occurs when <paramref name="targetImage"/> is <c>null</c></exception>
 public MemberCloner(MetadataImage targetImage)
 {
     if (targetImage == null)
     {
         throw new ArgumentNullException(nameof(targetImage));
     }
     _importer = new MemberClonerReferenceImporter(this, targetImage);
 }
Exemplo n.º 2
0
        private static MethodDefinition CreateMainMethod(
            MetadataImage image,
            IReferenceImporter importer,
            IMemberReference getSecretNumberMethod)
        {
            var mainMethod = new MethodDefinition("Main", MethodAttributes.Public | MethodAttributes.Static,
                                                  new MethodSignature(image.TypeSystem.Void));

            var cilBody   = new CilMethodBody(mainMethod);
            var writeLine = importer.ImportMethod(
                typeof(Console).GetMethod("WriteLine", new[] { typeof(string), typeof(object) }));

            cilBody.Instructions.AddRange(new[]
            {
                CilInstruction.Create(CilOpCodes.Ldstr, "The secret number is: {0}"),
                CilInstruction.Create(CilOpCodes.Call, getSecretNumberMethod),
                CilInstruction.Create(CilOpCodes.Box, importer.ImportType(typeof(int))),
                CilInstruction.Create(CilOpCodes.Call, writeLine),
                CilInstruction.Create(CilOpCodes.Ret)
            });
            mainMethod.MethodBody = cilBody;
            return(mainMethod);
        }