Skip to content

Commit a5ad9ca

Browse files
pergerchDresel
authored andcommitted
Fixed crash when multiple cache properties present
1 parent 273417c commit a5ad9ca

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

src/SpatialFocus.MethodCache.Fody/Extensions/TypeDefinitionExtension.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace SpatialFocus.MethodCache.Fody.Extensions
66
{
77
using System;
8+
using System.Collections.Generic;
89
using System.Linq;
910
using Mono.Cecil;
1011

@@ -39,12 +40,12 @@ public static bool IsEligibleForWeaving(this TypeDefinition typeDefinition, Refe
3940
throw new ArgumentNullException(nameof(references));
4041
}
4142

42-
bool isEligibleForWeaving = typeDefinition.TryGetCacheGetterProperty(references) != null;
43+
bool isEligibleForWeaving = typeDefinition.TryGetCacheGetterProperty(references)?.Count == 1;
4344

4445
return isEligibleForWeaving;
4546
}
4647

47-
public static PropertyDefinition TryGetCacheGetterProperty(this TypeDefinition typeDefinition, References references)
48+
public static List<PropertyDefinition> TryGetCacheGetterProperty(this TypeDefinition typeDefinition, References references)
4849
{
4950
if (typeDefinition == null)
5051
{
@@ -56,7 +57,7 @@ public static PropertyDefinition TryGetCacheGetterProperty(this TypeDefinition t
5657
throw new ArgumentNullException(nameof(references));
5758
}
5859

59-
PropertyDefinition property = typeDefinition.Properties.Where(propertyDefinition =>
60+
List<PropertyDefinition> properties = typeDefinition.Properties.Where(propertyDefinition =>
6061
{
6162
TypeDefinition propertyTypeDefinition = propertyDefinition.PropertyType.Resolve();
6263
TypeDefinition memoryCacheInterface = references.MemoryCacheInterface.Resolve();
@@ -78,15 +79,15 @@ public static PropertyDefinition TryGetCacheGetterProperty(this TypeDefinition t
7879

7980
return false;
8081
})
81-
.SingleOrDefault();
82+
.ToList();
8283

83-
if (property == null && typeDefinition.BaseType != null)
84+
if (!properties.Any() && typeDefinition.BaseType != null)
8485
{
8586
TypeDefinition baseTypeDefinition = typeDefinition.BaseType.Resolve();
86-
property = baseTypeDefinition.TryGetCacheGetterProperty(references);
87+
properties = baseTypeDefinition.TryGetCacheGetterProperty(references);
8788
}
8889

89-
return property;
90+
return properties;
9091
}
9192

9293
public static CustomAttribute TryGetCacheAttribute(this TypeDefinition typeDefinition, References references)

src/SpatialFocus.MethodCache.Fody/MemoryCache.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ public static MethodReference GetCacheGetterMethod(ClassWeavingContext classWeav
5151
}
5252

5353
// TODO: Check if this can be inherited, extract to class level
54-
PropertyDefinition propertyDefinition =
54+
List<PropertyDefinition> propertyDefinitions =
5555
classWeavingContext.TypeDefinition.TryGetCacheGetterProperty(classWeavingContext.References);
5656

5757
// TODO: Also check fields
58-
if (propertyDefinition == null)
58+
if (propertyDefinitions == null || propertyDefinitions.Count != 1)
5959
{
60-
throw new WeavingException("Property not found");
60+
throw new WeavingException("Cache Property not found or multiple properties found.");
6161
}
6262

63-
MethodDefinition methodDefinition = propertyDefinition.GetMethod;
63+
MethodDefinition methodDefinition = propertyDefinitions.Single().GetMethod;
6464

6565
if (methodDefinition.DeclaringType.GenericParameters.Any())
6666
{

src/SpatialFocus.MethodCache.Fody/ModuleWeaver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public override void Execute()
3232
if (!weavingCandidate.ClassDefinition.IsEligibleForWeaving(references))
3333
{
3434
WriteWarning(
35-
$"Class {weavingCandidate.ClassDefinition.Name} contains [Cache] attribute but does not contain a single non-inherited property implementing IMemoryCache interface");
35+
$"Class {weavingCandidate.ClassDefinition.Name} contains [Cache] attribute but does not contain a single property implementing IMemoryCache interface");
3636
continue;
3737
}
3838

0 commit comments

Comments
 (0)