Example #1
0
        public void CompileSignature(ModuleBuilder module, CompileContext context)
        {
            string name = this.Name;

            if (this.Variables.Count > 0)
            {
                name += "_" + this.Variables.Count;
            }

            TypeInfo           newTypeInfo = new TypeInfo();
            TypeBuilder        type        = module.DefineType(name, TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.BeforeFieldInit, typeof(ProcessBase));
            ConstructorBuilder constructor;

            newTypeInfo.Builder = type;

            //If there are variables then define a type for them ...
            if (this.Variables.Count > 0)
            {
                foreach (Variable v in this.Variables)
                {
                    if (context.Options.Optimize && !v.IsUsed)
                    {
                        continue;
                    }
                    FieldBuilder field = type.DefineField(v.Name, typeof(object), FieldAttributes.Assembly);
                    newTypeInfo.AddField(field);
                }

                Type[] paramTypes = new Type[this.Variables.Count];
                for (int i = 0; i < paramTypes.Length; i++)
                {
                    paramTypes[i] = typeof(object);
                }
                constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, paramTypes);
                ILGenerator     ilCon   = constructor.GetILGenerator();
                ConstructorInfo conBase = typeof(ProcessBase).GetConstructor(new Type[] { });
                ilCon.Emit(OpCodes.Ldarg_0);
                ilCon.Emit(OpCodes.Call, conBase);

                //For every variable in the constructor, set it on the corresponding field
                for (int i = 0; i < this.Variables.Count; i++)
                {
                    Variable var = (Variable)this.Variables[i];
                    constructor.DefineParameter(i + 1, ParameterAttributes.None, var.Name);
                    newTypeInfo.ConstructorParameters.Add(var.Name);

                    if (context.Options.Optimize && !var.IsUsed)
                    {
                        continue;
                    }
                    ilCon.Emit(OpCodes.Ldarg_0);
                    ilCon.Emit(OpCodes.Ldarg, i + 1);
                    ilCon.Emit(OpCodes.Stfld, newTypeInfo.GetField(var.Name));
                }
                ilCon.Emit(OpCodes.Ret);
                newTypeInfo.Constructor = constructor;
            }
            else
            {
                newTypeInfo.Constructor = type.DefineDefaultConstructor(MethodAttributes.Public);
            }

            context.AddType(newTypeInfo);
        }