Skip to content

Commit 273417c

Browse files
pergerchDresel
authored andcommitted
Support for inherited IMemoryCache property, closes #2
1 parent 43b6948 commit 273417c

4 files changed

Lines changed: 80 additions & 27 deletions

File tree

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,24 @@ public static bool IsEligibleForWeaving(this TypeDefinition typeDefinition, Refe
3939
throw new ArgumentNullException(nameof(references));
4040
}
4141

42-
return typeDefinition.Properties.Where(propertyDefinition =>
42+
bool isEligibleForWeaving = typeDefinition.TryGetCacheGetterProperty(references) != null;
43+
44+
return isEligibleForWeaving;
45+
}
46+
47+
public static PropertyDefinition TryGetCacheGetterProperty(this TypeDefinition typeDefinition, References references)
48+
{
49+
if (typeDefinition == null)
50+
{
51+
throw new ArgumentNullException(nameof(typeDefinition));
52+
}
53+
54+
if (references == null)
55+
{
56+
throw new ArgumentNullException(nameof(references));
57+
}
58+
59+
PropertyDefinition property = typeDefinition.Properties.Where(propertyDefinition =>
4360
{
4461
TypeDefinition propertyTypeDefinition = propertyDefinition.PropertyType.Resolve();
4562
TypeDefinition memoryCacheInterface = references.MemoryCacheInterface.Resolve();
@@ -61,7 +78,15 @@ public static bool IsEligibleForWeaving(this TypeDefinition typeDefinition, Refe
6178

6279
return false;
6380
})
64-
.Count() == 1;
81+
.SingleOrDefault();
82+
83+
if (property == null && typeDefinition.BaseType != null)
84+
{
85+
TypeDefinition baseTypeDefinition = typeDefinition.BaseType.Resolve();
86+
property = baseTypeDefinition.TryGetCacheGetterProperty(references);
87+
}
88+
89+
return property;
6590
}
6691

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

src/SpatialFocus.MethodCache.Fody/MemoryCache.cs

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

5353
// TODO: Check if this can be inherited, extract to class level
54-
List<PropertyDefinition> propertyDefinitions = classWeavingContext.TypeDefinition.Properties.Where(definition =>
55-
{
56-
TypeDefinition typeDefinition = definition.PropertyType.Resolve();
57-
TypeDefinition memoryCacheInterface = classWeavingContext.References.MemoryCacheInterface.Resolve();
58-
59-
if (definition.GetMethod.IsStatic)
60-
{
61-
return false;
62-
}
63-
64-
if (typeDefinition.IsInterface && typeDefinition.Equals(memoryCacheInterface))
65-
{
66-
return true;
67-
}
68-
69-
if (typeDefinition.Interfaces.Any(x => x.InterfaceType == memoryCacheInterface))
70-
{
71-
return true;
72-
}
73-
74-
return false;
75-
})
76-
.ToList();
54+
PropertyDefinition propertyDefinition =
55+
classWeavingContext.TypeDefinition.TryGetCacheGetterProperty(classWeavingContext.References);
7756

7857
// TODO: Also check fields
79-
if (propertyDefinitions.Count != 1)
58+
if (propertyDefinition == null)
8059
{
8160
throw new WeavingException("Property not found");
8261
}
8362

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

8665
if (methodDefinition.DeclaringType.GenericParameters.Any())
8766
{
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// <copyright file="DerivedTestClass.cs" company="Spatial Focus GmbH">
2+
// Copyright (c) Spatial Focus GmbH. All rights reserved.
3+
// </copyright>
4+
5+
namespace SpatialFocus.MethodCache.TestAssembly
6+
{
7+
using Microsoft.Extensions.Caching.Memory;
8+
9+
[Cache]
10+
public class DerivedTestClass : BaseClass
11+
{
12+
public DerivedTestClass(IMemoryCache memoryCache) : base(memoryCache)
13+
{
14+
}
15+
16+
#pragma warning disable CA1822 // Mark members as static
17+
public int Add(int a, int b)
18+
{
19+
return a + b;
20+
}
21+
#pragma warning restore CA1822 // Mark members as static
22+
}
23+
24+
#pragma warning disable SA1402 // File may only contain a single type
25+
public class BaseClass
26+
{
27+
public BaseClass(IMemoryCache memoryCache)
28+
{
29+
MemoryCache = memoryCache;
30+
}
31+
32+
public IMemoryCache MemoryCache { get; }
33+
}
34+
#pragma warning restore SA1402 // File may only contain a single type
35+
}

src/SpatialFocus.MethodCache.Tests/MemoryCacheBasicTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,19 @@ public void BasicTest4NoCache()
8383
Assert.Equal(0, mockMemoryCache.CountSets);
8484
Assert.Equal(0, mockMemoryCache.CountGets);
8585
}
86+
87+
[Fact]
88+
public void BasicTest5DerivedClass()
89+
{
90+
using MockMemoryCache mockMemoryCache = new MockMemoryCache();
91+
92+
dynamic instance = TestHelpers.CreateInstance<DerivedTestClass>(MemoryCacheBasicTests.TestResult.Assembly, mockMemoryCache);
93+
94+
dynamic result = instance.Add(1, 2);
95+
96+
Assert.Equal(3, result);
97+
Assert.Equal(1, mockMemoryCache.CountSets);
98+
Assert.Equal(1, mockMemoryCache.CountGets);
99+
}
86100
}
87101
}

0 commit comments

Comments
 (0)