DCSIMG
Intercept.It new drop - including Generics - IHateSpaghetti {code}

IHateSpaghetti {code}

VSX, DSL and Beyond by Eyal Lantzman

Syndication

Coding / Architecture

Extensibility /DSL

Projects

Articles

Intercept.It new drop - including Generics

The new bits for my AOP framework.

The changes for the helper class were relatively minimal.

Method creation:

   1: public IGenerationContext CreateMethod(TypeBuilder typeBuilder, MethodInfo method)
   2: {
   3:     Type[] parameterTypes = GetParameterTypes(method);
   4:     MethodAttributes attributes = MethodAttributes.Public;
   5:     if (!method.IsFinal)
   6:         attributes |= MethodAttributes.Virtual;
   7:  
   8:     MethodBuilder methodInfoBody = typeBuilder.DefineMethod(
   9:         method.Name,
  10:         attributes,
  11:         method.CallingConvention,
  12:         method.ReturnType,
  13:         parameterTypes);
  14:    
  15:     methodInfoBody.InitLocals = true;
  16:  
  17:     if (method.IsGenericMethodDefinition)
  18:     {
  19:         Type[] genericParameterTypes = method.GetGenericArguments();
  20:         string[] names = new string[genericParameterTypes.Length];
  21:         for (int i = 0; i < genericParameterTypes.Length; i++)
  22:             names[i] = "T" + i.ToString();
  23:         GenericTypeParameterBuilder[] genericParams = methodInfoBody.DefineGenericParameters(names);
  24:     }
  25:     ILGenerator generator = methodInfoBody.GetILGenerator();
  26:     return GenerationContext.SetCurrentContext(typeBuilder,generator, method);
  27: }

Array population:

   1: public Helper PopulateArray(IVariable arrayInstance, Type arrayItemType, Type[] parametersTypes, IVariable[] parameters, int offset, int length)
   2:        {
   3:            if (parametersTypes.Length != parameters.Length)
   4:                throw new InvalidOperationException("the number of parameter types is inconsistent with parameters number");
   5:  
   6:            int processed = 0;
   7:            for (int i = offset; i < parametersTypes.Length && processed < length; i++)
   8:            {
   9:                arrayInstance.Load();
  10:                GenerationContext.CurrentContext.Generator.Emit(OpCodes.Ldc_I4, i);           //load the array item i
  11:  
  12:                parameters[i].Load(); //load the variable
  13:  
  14:  
  15:                if (!arrayItemType.IsByRef)
  16:                {
  17:                    if (parametersTypes[i].IsValueType || parametersTypes[i].IsGenericParameter)
  18:                        GenerationContext.CurrentContext.Generator.Emit(OpCodes.Box, parametersTypes[i]);      //box value types
  19:  
  20:                }
  21:                GenerationContext.CurrentContext.Generator.Emit(OpCodes.Stelem_Ref);   //replace array item i
  22:  
  23:            }
  24:            return this;
  25:        }

I made a couple of more changes see the helper class for more information. 

 

There are 2 advice classes - feel free to add more.

 

P.S if you want to help me with this project drop me a note (if have ideas for couple of new features that might be useful).

 

That's it for this time as before you can download the relevant change set (38824) or browse the code in this project's codeplex site.

Published Wednesday, September 03, 2008 1:50 PM by Eyal

Comments

# Websites tagged "opcodes" on Postsaver@ Monday, February 16, 2009 3:17 PM

Pingback from  Websites tagged "opcodes" on Postsaver

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: