Skip to content

Commit 701c8bf

Browse files
author
Oliver Ehrenmüller
committed
Check for duplicate add
1 parent 02649f3 commit 701c8bf

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

DeepCopy.Fody/ModuleWeaver.cs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,35 @@ private void AddDeepCopyConstructors(IEnumerable<TypeDefinition> targets)
5757
foreach (var target in targets)
5858
Run(target, () =>
5959
{
60-
if (target.HasCopyConstructor(out var constructor))
60+
var hasAttribute = target.TryRemove(DeepCopyAttribute.AddDeepCopyConstructor, out var attribute);
61+
if (!target.HasCopyConstructor(out var constructor))
6162
{
62-
if (target.Has(DeepCopyAttribute.AddDeepCopyConstructor, out var attribute)
63-
&& attribute.GetArgument("Overwrite", false)
64-
|| OverwriteByDefault)
65-
{
66-
AddDeepConstructor(target, ModuleDefinition.ImportReference(constructor).Resolve());
67-
}
68-
else if (!constructor.Resolve().IsPublic)
69-
{
70-
if (!OverwriteByDefault)
71-
WriteWarning($"Non-public constructor for {target.FullName} will be overwritten");
72-
AddDeepConstructor(target, ModuleDefinition.ImportReference(constructor).Resolve());
73-
}
74-
else
75-
throw new WeavingException(@"Type already has a copy constructor
76-
- Use [DeepCopyConstructor] on constructor to inject deep copy code
77-
- Use [AddDeepCopyConstructor(Overwrite=true)] on type to replace existing constructor
78-
- Set global config <DeepCopy OverwriteByDefault=""True"" /> in FodyWeavers.xml");
63+
AddDeepConstructor(target, null);
64+
return;
7965
}
80-
else
66+
67+
var constructorResolved = constructor.Resolve();
68+
if (constructorResolved.Has(DeepCopyAttribute.DeepCopyConstructor)
69+
|| constructorResolved.Has(DeepCopyAttribute.InjectDeepCopy))
70+
return;
71+
72+
if (hasAttribute
73+
&& attribute.GetArgument("Overwrite", false)
74+
|| OverwriteByDefault)
8175
{
82-
AddDeepConstructor(target, null);
76+
AddDeepConstructor(target, ModuleDefinition.ImportReference(constructor).Resolve());
77+
}
78+
else if (!constructorResolved.IsPublic)
79+
{
80+
if (!OverwriteByDefault)
81+
WriteWarning($"Non-public constructor for {target.FullName} will be overwritten");
82+
AddDeepConstructor(target, ModuleDefinition.ImportReference(constructor).Resolve());
8383
}
84-
target.TryRemove(DeepCopyAttribute.AddDeepCopyConstructor);
84+
else
85+
throw new WeavingException(@"Type already has a copy constructor
86+
- Use [DeepCopyConstructor] on constructor to inject deep copy code
87+
- Use [AddDeepCopyConstructor(Overwrite=true)] on type to replace existing constructor
88+
- Set global config <DeepCopy OverwriteByDefault=""True"" /> in FodyWeavers.xml");
8589
});
8690
}
8791

DeepCopy.Fody/Utils/AttributeExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ public static bool Has(this ICustomAttributeProvider attributeProvider, DeepCopy
3333

3434
public static bool TryRemove(this ICustomAttributeProvider attributeProvider, DeepCopyAttribute name)
3535
{
36-
if (!attributeProvider.Has(name, out var attribute))
36+
return TryRemove(attributeProvider, name, out _);
37+
}
38+
39+
public static bool TryRemove(this ICustomAttributeProvider attributeProvider, DeepCopyAttribute name, out CustomAttribute attribute)
40+
{
41+
if (!attributeProvider.Has(name, out attribute))
3742
return false;
3843
attributeProvider.CustomAttributes.Remove(attribute);
3944
return true;

0 commit comments

Comments
 (0)