Ejemplo n.º 1
0
        private void InitAspectFields(CustomAttribute aspect, VariableDefinition aspectInstanceVar)
        {
            var ctor = initCtor.Body.Instructions;

            // init all array-type fields as local variables first
            var arrayVars = new Dictionary <CustomAttributeNamedArgument, VariableDefinition>();

            foreach (var field in aspect.Fields.Where(a => a.Argument.Type.IsArray))
            {
                arrayVars.Add(field, AddInitArrayCode(field.Argument.Type, (Array)field.Argument.Value));
            }

            // set field values
            // aspectVar.field1 = const1;
            foreach (var field in aspect.Fields)
            {
                ctor.Add(OpCodes.Ldloc, aspectInstanceVar);
                if (field.Argument.Type.IsArray)
                {
                    ctor.Add(OpCodes.Ldloc, arrayVars[field]);
                }
                else
                {
                    ctor.Add(ILTools.GetLdcOpCode(field.Argument.Type, field.Argument.Value));
                }

                var fieldDef = aspectInstanceVar.VariableType.FindField(field.Name);
                ctor.Add(OpCodes.Stfld, initCtor.Module.Import(fieldDef));
            }
        }
Ejemplo n.º 2
0
        private void InitAspectProperties(CustomAttribute aspect, VariableDefinition aspectInstanceVar)
        {
            var ctor = initCtor.Body.Instructions;

            // init all array-type properties as local variables first
            var arrayVars = new Dictionary <CustomAttributeNamedArgument, VariableDefinition>();

            foreach (var prop in aspect.Properties.Where(a => a.Argument.Type.IsArray))
            {
                arrayVars.Add(prop, AddInitArrayCode(prop.Argument.Type, (Array)prop.Argument.Value));
            }

            // set field values
            // aspectVar.field1 = const1;
            foreach (var prop in aspect.Properties)
            {
                ctor.Add(OpCodes.Ldloc, aspectInstanceVar);
                if (prop.Argument.Type.IsArray)
                {
                    ctor.Add(OpCodes.Ldloc, arrayVars[prop]);
                }
                else
                {
                    ctor.Add(ILTools.GetLdcOpCode(prop.Argument.Type, prop.Argument.Value));
                }

                var propSetMethod = aspectInstanceVar.VariableType.FindProperty(prop.Name).SetMethod;
                ctor.Add(OpCodes.Callvirt, initCtor.Module.Import(propSetMethod));
            }
        }
Ejemplo n.º 3
0
        private VariableDefinition CreateAspectInstance(CustomAttribute aspect)
        {
            var ctor = initCtor.Body.Instructions;

            // init all array-type parameters as local variables first
            var arrayVars = new Dictionary <CustomAttributeArgument, VariableDefinition>();

            foreach (var arg in aspect.ConstructorArguments.Where(a => a.Type.IsArray))
            {
                arrayVars.Add(arg, AddInitArrayCode(arg.Type, (Array)arg.Value));
            }

            // call aspect's contructor
            // var aspect = new AspectClass (param1, param2, ...)
            foreach (var arg in aspect.ConstructorArguments)
            {
                if (arg.Type.IsArray)
                {
                    ctor.Add(OpCodes.Ldloc, arrayVars[arg]);
                }
                else
                {
                    ctor.Add(ILTools.GetLdcOpCode(arg.Type, arg.Value));
                }
            }
            ctor.Add(OpCodes.Newobj, aspect.Constructor);

            var aspectInstanceVar = new VariableDefinition(aspect.AttributeType);

            ctor.Add(OpCodes.Stloc, aspectInstanceVar);
            initCtor.Body.Variables.Add(aspectInstanceVar);

            return(aspectInstanceVar);
        }
Ejemplo n.º 4
0
        private VariableDefinition AddInitArrayCode(TypeReference type, Array value)
        {
            if (value.Rank != 1)
            {
                throw new ArgumentException("Only 1-dimension arrays are supported", "value");
            }

            var ctor     = initCtor.Body.Instructions;
            var elemType = type.GetElementType();

            // Generates code:
            // var arrayVar = new ArrayType[n];
            // arrayVar[0] = const1;
            // arrayVar[1] = const2;
            // ...

            var arrayVar = new VariableDefinition(type);

            initCtor.Body.Variables.Add(arrayVar);

            int n = value.GetLength(0);

            ctor.Add(OpCodes.Ldc_I4, n);
            ctor.Add(OpCodes.Newarr, elemType);
            ctor.Add(OpCodes.Stloc, arrayVar);

            int startIndex = value.GetLowerBound(0);

            for (int i = 0; i < n; ++i)
            {
                ctor.Add(OpCodes.Ldloc, arrayVar);
                ctor.Add(OpCodes.Ldc_I4, i + startIndex);
                ctor.Add(ILTools.GetLdcOpCode(elemType, ((CustomAttributeArgument)value.GetValue(i + startIndex)).Value));
                ctor.Add(ILTools.GetStelemOpCode(elemType));
            }

            return(arrayVar);
        }