コード例 #1
0
ファイル: ValueTarget.cs プロジェクト: s-krawczyk/DeepCopy
        public IDisposable Build(ICollection <Instruction> instructions)
        {
            _instructions = instructions;
            if (_variable == null)
            {
                instructions.Add(_instance != null
                    ? Instruction.Create(OpCodes.Ldloc, _instance)
                    : Instruction.Create(OpCodes.Ldarg_0));
            }

            if (_index != null)
            {
                if (_variable != null)
                {
                    _instructions.Add(_variable.CreateLoadInstruction());
                }
                else if (_property != null)
                {
                    instructions.Add(_property.CreateGetInstruction());
                }
                _instructions.Add(Instruction.Create(OpCodes.Ldloc, _index));

                return(this);
            }

            return(this);
        }
コード例 #2
0
        private IEnumerable <Instruction> CopyDictionary(TypeReference type, ValueSource source, ValueTarget target)
        {
            var typesOfArguments = type.GetGenericArguments();
            var typeKeyValuePair = ImportType(typeof(KeyValuePair <,>), typesOfArguments);

            var list = new List <Instruction>();

            using (new IfNotNull(list, source, target.IsTargetingBase))
            {
                VariableDefinition variable = null;
                if (!target.IsTargetingBase)
                {
                    list.AddRange(NewInstance(type, typeof(IDictionary <,>), typeof(Dictionary <,>), out variable));
                }

                using (var forEach = new ForEach(this, list, type, source))
                {
                    var sourceKey   = ValueSource.New().Variable(forEach.Current).Method(ImportMethod(typeKeyValuePair, "get_Key", typesOfArguments));
                    var sourceValue = ValueSource.New().Variable(forEach.Current).Method(ImportMethod(typeKeyValuePair, "get_Value", typesOfArguments));

                    var targetKey = NewVariable(typesOfArguments[0]);
                    list.AddRange(Copy(typesOfArguments[0], sourceKey, ValueTarget.New().Variable(targetKey)));
                    var targetValue = NewVariable(typesOfArguments[1]);
                    list.AddRange(Copy(typesOfArguments[1], sourceValue, ValueTarget.New().Variable(targetValue)));

                    list.Add(variable?.CreateLoadInstruction() ?? Instruction.Create(OpCodes.Ldarg_0));
                    list.Add(targetKey.CreateLoadInstruction());
                    list.Add(targetValue.CreateLoadInstruction());
                    list.Add(Instruction.Create(OpCodes.Callvirt, ImportMethod(type.Resolve(), "set_Item", typesOfArguments)));
                }

                if (!target.IsTargetingBase)
                {
                    list.AddRange(target.Build(ValueSource.New().Variable(variable)));
                }
            }

            return(list);
        }