Skip to content

Commit 6fd138a

Browse files
committed
fix für key-copy in dictionary
1 parent 01f2cdd commit 6fd138a

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

DeepCopy.Fody/CopyDictionary.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ private IEnumerable<Instruction> CopyDictionary(TypeReference type, ValueSource
2424
var sourceKey = ValueSource.New().Variable(forEach.Current).Method(ImportMethod(typeKeyValuePair, "get_Key", typesOfArguments));
2525
var sourceValue = ValueSource.New().Variable(forEach.Current).Method(ImportMethod(typeKeyValuePair, "get_Value", typesOfArguments));
2626

27-
var valueTarget = NewVariable(typesOfArguments[1]);
28-
list.AddRange(Copy(typesOfArguments[1], sourceValue, ValueTarget.New().Variable(valueTarget)));
27+
var targetKey = NewVariable(typesOfArguments[0]);
28+
list.AddRange(Copy(typesOfArguments[0], sourceKey, ValueTarget.New().Variable(targetKey)));
29+
var targetValue = NewVariable(typesOfArguments[1]);
30+
list.AddRange(Copy(typesOfArguments[1], sourceValue, ValueTarget.New().Variable(targetValue)));
2931

3032
list.Add(variable?.CreateLoadInstruction() ?? Instruction.Create(OpCodes.Ldarg_0));
31-
list.AddRange(sourceKey);
32-
list.Add(valueTarget.CreateLoadInstruction());
33+
list.Add(targetKey.CreateLoadInstruction());
34+
list.Add(targetValue.CreateLoadInstruction());
3335
list.Add(Instruction.Create(OpCodes.Callvirt, ImportMethod(type.Resolve(), "set_Item", typesOfArguments)));
3436
}
3537

DeepCopy.Fody/Utils/ForEach.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public ForEach(ModuleWeaver moduleWeaver, List<Instruction> instructions, TypeRe
4444

4545
// try
4646
_startCondition = Instruction.Create(OpCodes.Ldloc, _enumerator);
47-
_startTry = Instruction.Create(OpCodes.Br_S, _startCondition);
47+
_startTry = Instruction.Create(OpCodes.Br, _startCondition);
4848
_instructions.Add(_startTry);
4949

5050
_startLoop = Instruction.Create(OpCodes.Ldloc, _enumerator);
@@ -57,7 +57,7 @@ public void Dispose()
5757
{
5858
_instructions.Add(_startCondition);
5959
_instructions.Add(Instruction.Create(OpCodes.Callvirt, _moduleWeaver.ImportMethod(_typeEnumerator, nameof(IEnumerator.MoveNext))));
60-
_instructions.Add(Instruction.Create(OpCodes.Brtrue_S, _startLoop));
60+
_instructions.Add(Instruction.Create(OpCodes.Brtrue, _startLoop));
6161

6262
// end try
6363
var end = Instruction.Create(OpCodes.Nop);

Tests/CopyDictionaryTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ public void TestClassWithDictionaryObject()
7979
Assert.NotSame(instance.Dictionary, copy.Dictionary);
8080
Assert.NotSame(instance.Dictionary[someKey1], copy.Dictionary[someKey1]);
8181
Assert.NotSame(instance.Dictionary[someKey2], copy.Dictionary[someKey2]);
82+
83+
var instanceKey1 = System.Linq.Enumerable.First(instance.Dictionary.Keys);
84+
var copyKey1 = System.Linq.Enumerable.First(copy.Dictionary.Keys);
85+
Assert.Equal(instanceKey1, copyKey1);
86+
Assert.NotSame(instanceKey1, copyKey1);
8287
}
8388

8489
[Fact]

0 commit comments

Comments
 (0)