Skip to content

Commit aedc42e

Browse files
committed
#8 fix für auto property initializer
1 parent 92c2c0e commit aedc42e

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

AssemblyToProcess/SomeObject.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,20 @@ public override int GetHashCode()
5656

5757
#endregion
5858
}
59+
60+
[AddDeepCopyConstructor]
61+
public class AutoPropertyInitializerObject
62+
{
63+
public Guid Guid { get; set; } = Guid.NewGuid();
64+
}
65+
66+
public class AutoPropertyInitializerConstructorObject
67+
{
68+
public AutoPropertyInitializerConstructorObject() { }
69+
70+
[InjectDeepCopy]
71+
public AutoPropertyInitializerConstructorObject(AutoPropertyInitializerConstructorObject source) { }
72+
73+
public Guid Guid { get; set; } = Guid.NewGuid();
74+
}
5975
}

DeepCopy.Fody/ModuleWeaver.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private void ExecuteInjectDeepCopy()
104104

105105
var constructorResolved = constructor.Resolve();
106106
constructorResolved.Body.SimplifyMacros();
107-
InsertCopyInstructions(target, constructorResolved.Body, 2);
107+
InsertCopyInstructions(target, constructorResolved.Body);
108108
constructorResolved.CustomAttributes.Remove(constructorResolved.SingleAttribute(InjectDeepCopyAttribute));
109109
}
110110
}
@@ -115,7 +115,6 @@ private void AddDeepConstructor(TypeDefinition type)
115115
constructor.Parameters.Add(new ParameterDefinition(type));
116116

117117
var processor = constructor.Body.GetILProcessor();
118-
var offset = 2;
119118

120119
if (type.BaseType.Resolve().MetadataToken == TypeSystem.ObjectDefinition.MetadataToken)
121120
{
@@ -127,24 +126,24 @@ private void AddDeepConstructor(TypeDefinition type)
127126
processor.Emit(OpCodes.Ldarg_0);
128127
processor.Emit(OpCodes.Ldarg_1);
129128
processor.Emit(OpCodes.Call, baseConstructor);
130-
offset = 3;
131129
}
132130
else
133131
throw new CopyConstructorRequiredException(type.BaseType);
134132

135-
InsertCopyInstructions(type, constructor.Body, offset);
133+
InsertCopyInstructions(type, constructor.Body);
136134

137135
processor.Emit(OpCodes.Ret);
138136
type.Methods.Add(constructor);
139137
}
140138

141-
private void InsertCopyInstructions(TypeDefinition type, MethodBody body, int offset)
139+
private void InsertCopyInstructions(TypeDefinition type, MethodBody body)
142140
{
143141
_booleanVariable = null;
144142
_indexVariable = null;
145143
CurrentBody.Value = body;
146144

147-
var index = offset;
145+
var baseConstructorCall = body.Instructions.Single(i => i.OpCode == OpCodes.Call && i.Operand is MethodReference method && method.Name == ".ctor");
146+
var index = body.Instructions.IndexOf(baseConstructorCall) + 1;
148147
var properties = new List<string>();
149148

150149
foreach (var property in type.Properties)

Tests/CopyTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,29 @@ public void TestEmptyObject()
6060
var copy = Activator.CreateInstance(type, instance);
6161
Assert.NotSame(instance, copy);
6262
}
63+
64+
[Fact]
65+
public void TestAutoPropertyInitializer()
66+
{
67+
var type = GetTestType(typeof(AutoPropertyInitializerObject));
68+
dynamic instance = Activator.CreateInstance(type);
69+
var guid = Guid.NewGuid();
70+
instance.Guid = guid;
71+
var copy = Activator.CreateInstance(type, instance);
72+
Assert.NotSame(instance, copy);
73+
Assert.Equal(instance.Guid, copy.Guid);
74+
}
75+
76+
[Fact]
77+
public void TestAutoPropertyConstructorInitializer()
78+
{
79+
var type = GetTestType(typeof(AutoPropertyInitializerConstructorObject));
80+
dynamic instance = Activator.CreateInstance(type);
81+
var guid = Guid.NewGuid();
82+
instance.Guid = guid;
83+
var copy = Activator.CreateInstance(type, instance);
84+
Assert.NotSame(instance, copy);
85+
Assert.Equal(instance.Guid, copy.Guid);
86+
}
6387
}
6488
}

0 commit comments

Comments
 (0)