示例#1
0
 public IEnumerable <Instruction> Build(VariableDefinition variable) => Build(ValueSource.New().Variable(variable));
示例#2
0
        public ForEach(ModuleWeaver moduleWeaver, List <Instruction> instructions, TypeReference typeOfEnumerable, ValueSource source)
        {
            _moduleWeaver = moduleWeaver;
            _instructions = instructions;

            if (!typeOfEnumerable.TryFindImplementation(typeof(IEnumerable <>), out var typeOfEnumerableImpl))
            {
                throw new DeepCopyException($"{typeOfEnumerable.FullName} is no IEnumerable");
            }

            var typeOfCurrent = moduleWeaver.ImportType(((GenericInstanceType)typeOfEnumerableImpl).GenericArguments.Single());

            var methodGetEnumerator = moduleWeaver.ImportMethod(moduleWeaver.ImportType(typeof(IEnumerable <>), typeOfCurrent), nameof(IEnumerable.GetEnumerator), typeOfCurrent);

            _typeEnumerator = moduleWeaver.ImportType(methodGetEnumerator.ReturnType, typeOfCurrent);

            _enumerator = moduleWeaver.NewVariable(_typeEnumerator);
            Current     = moduleWeaver.NewVariable(typeOfCurrent);

            _instructions.AddRange(source);
            _instructions.Add(Instruction.Create(OpCodes.Callvirt, methodGetEnumerator));
            _instructions.Add(Instruction.Create(OpCodes.Stloc, _enumerator));

            // try
            _startCondition = Instruction.Create(OpCodes.Ldloc, _enumerator);
            _startTry       = Instruction.Create(OpCodes.Br, _startCondition);
            _instructions.Add(_startTry);

            _startLoop = Instruction.Create(OpCodes.Ldloc, _enumerator);
            _instructions.Add(_startLoop);
            _instructions.Add(Instruction.Create(OpCodes.Callvirt, moduleWeaver.ImportMethod(_typeEnumerator, CurrentGetter, typeOfCurrent)));
            _instructions.Add(Instruction.Create(OpCodes.Stloc, Current));
        }