Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press

Buy directly from Paradoxal Press at $33.99 (Save 43%)



Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA


Listing 7-21 extracted from chapter Reflection, late binding, attributes


Listing 7-20<     > Listing 8-1


This listing can be compiled with the command line:
csc.exe /target:exe Example_7_21.cs
Errors: 0 Warnings: 0


Example_7_21.cs
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;
using System.Diagnostics;

public interface IPolynome {
   int Eval(int x);
}
class Polynome {
   public IPolynome polynome;
   public Polynome(int[] coefs) {
      Assembly asm = BuildCodeInternal(coefs);
      polynome = (IPolynome)asm.CreateInstance("PolynomeInternal");
   }
   private Assembly BuildCodeInternal(int[] coefs) {
      AssemblyName asmName = new AssemblyName();
      asmName.Name = "EvalPolAsm";

      // Build an assembly dynamically.
      AssemblyBuilder asmBuilder =  
         Thread.GetDomain().DefineDynamicAssembly(
            asmName, AssemblyBuilderAccess.Run );

      // Add a module in the assembly.
      ModuleBuilder modBuilder = 
         asmBuilder.DefineDynamicModule("MainMod");

      // Ad the 'PolynomeInternal' class in the module.
      TypeBuilder typeBuilder = modBuilder.DefineType(
            "PolynomeInternal"TypeAttributes.Public);
      typeBuilder.AddInterfaceImplementation(typeof(IPolynome));

      // Implement the int Eval(int) method
      MethodBuilder methodBuilder = typeBuilder.DefineMethod(
            "Eval",
            MethodAttributes.Public | MethodAttributes.Virtual,
            typeof(int),                   // return type
            new Type[] { typeof(int) });   // argument

      // Generate the IL code from the table of coefs.
      ILGenerator ilGen = methodBuilder.GetILGenerator();
      int deg = coefs.GetLength(0);
      for (int = 0; i < deg - 1; i++) {
         ilGen.Emit(OpCodes.Ldc_I4, coefs[i]);
         ilGen.Emit(OpCodes.Ldarg, 1);
      }
      ilGen.Emit(OpCodes.Ldc_I4, coefs[deg - 1]);
      for (int = 0; i < deg - 1; i++) {
         ilGen.Emit(OpCodes.Mul);
         ilGen.Emit(OpCodes.Add);
      }
      ilGen.Emit(OpCodes.Ret);

      // Indicate that the 'Eval()' method is an implementation...
      // ... of the interface method 'IPolynome.Eval()'.
      MethodInfo methodInfo = typeof(IPolynome).GetMethod("Eval");
      typeBuilder.DefineMethodOverride(methodBuilder, methodInfo);
      typeBuilder.CreateType();
      return asmBuilder;
   }
}

class Program {
   static void Main() {
      Stopwatch sw = Stopwatch.StartNew();
      int[] coefs = 30139, -137358366 };
      Polynome p = new Polynome(coefs);
      for (int = -26; x <= 19; x++)
         for (int = 0; i < 10000000; i++)
            p.polynome.Eval(x);
      Console.WriteLine("Duration:" + sw.Elapsed);
   }
}	
Copyright Patrick Smacchia 2006 2007