Skip to content

Commit 8c1b149

Browse files
committed
Import cache getter to enable import from base classes in other assemblies
1 parent ce41992 commit 8c1b149

File tree

7 files changed

+75
-1
lines changed

7 files changed

+75
-1
lines changed

SpatialFocus.MethodCache.Fody.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpatialFocus.MethodCache.Sa
3838
{81703F57-025F-42C4-AE8B-568E2F56CCE2} = {81703F57-025F-42C4-AE8B-568E2F56CCE2}
3939
EndProjectSection
4040
EndProject
41+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpatialFocus.MethodCache.TestAssembly2", "src\SpatialFocus.MethodCache.TestAssembly2\SpatialFocus.MethodCache.TestAssembly2.csproj", "{766C91B4-5753-4B31-9FBF-684D6754D2DA}"
42+
EndProject
4143
Global
4244
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4345
Debug|Any CPU = Debug|Any CPU
@@ -72,6 +74,10 @@ Global
7274
{E03D9328-B71F-4451-9956-6D28186F84A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
7375
{E03D9328-B71F-4451-9956-6D28186F84A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
7476
{E03D9328-B71F-4451-9956-6D28186F84A7}.Release|Any CPU.Build.0 = Release|Any CPU
77+
{766C91B4-5753-4B31-9FBF-684D6754D2DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
78+
{766C91B4-5753-4B31-9FBF-684D6754D2DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
79+
{766C91B4-5753-4B31-9FBF-684D6754D2DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
80+
{766C91B4-5753-4B31-9FBF-684D6754D2DA}.Release|Any CPU.Build.0 = Release|Any CPU
7581
EndGlobalSection
7682
GlobalSection(SolutionProperties) = preSolution
7783
HideSolutionNode = FALSE
@@ -84,6 +90,7 @@ Global
8490
{C47814DC-272E-4FB8-A97C-2B6C75F60C76} = {674F705E-E459-490B-B536-3F750485AE33}
8591
{88A8C843-A479-42B8-AAF6-8F816C660515} = {674F705E-E459-490B-B536-3F750485AE33}
8692
{E03D9328-B71F-4451-9956-6D28186F84A7} = {674F705E-E459-490B-B536-3F750485AE33}
93+
{766C91B4-5753-4B31-9FBF-684D6754D2DA} = {674F705E-E459-490B-B536-3F750485AE33}
8794
EndGlobalSection
8895
GlobalSection(ExtensibilityGlobals) = postSolution
8996
SolutionGuid = {1a3946bb-e6d6-4999-932a-807a80bc0cdb}

src/SpatialFocus.MethodCache.Fody/MemoryCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static MethodReference GetCacheGetterMethod(ClassWeavingContext classWeav
6060
throw new WeavingException("Cache Property not found or multiple properties found.");
6161
}
6262

63-
MethodDefinition methodDefinition = propertyDefinitions.Single().GetMethod;
63+
MethodReference methodDefinition = classWeavingContext.TypeDefinition.Module.ImportReference(propertyDefinitions.Single().GetMethod);
6464

6565
if (methodDefinition.DeclaringType.GenericParameters.Any())
6666
{
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// <copyright file="DerivedTestClass2.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 DerivedTestClass2 : TestAssembly2.BaseClass
11+
{
12+
public DerivedTestClass2(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+
}

src/SpatialFocus.MethodCache.TestAssembly/SpatialFocus.MethodCache.TestAssembly.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</ItemGroup>
1111

1212
<ItemGroup>
13+
<ProjectReference Include="..\SpatialFocus.MethodCache.TestAssembly2\SpatialFocus.MethodCache.TestAssembly2.csproj" />
1314
<ProjectReference Include="..\SpatialFocus.MethodCache\SpatialFocus.MethodCache.csproj" />
1415
</ItemGroup>
1516

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// <copyright file="BaseClass.cs" company="Spatial Focus GmbH">
2+
// Copyright (c) Spatial Focus GmbH. All rights reserved.
3+
// </copyright>
4+
5+
namespace SpatialFocus.MethodCache.TestAssembly2
6+
{
7+
using Microsoft.Extensions.Caching.Memory;
8+
9+
public class BaseClass
10+
{
11+
public BaseClass(IMemoryCache memoryCache)
12+
{
13+
MemoryCache = memoryCache;
14+
}
15+
16+
public IMemoryCache MemoryCache { get; }
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="3.1.9" />
9+
</ItemGroup>
10+
11+
</Project>

src/SpatialFocus.MethodCache.Tests/MemoryCacheBasicTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,19 @@ public void BasicTest5DerivedClass()
9797
Assert.Equal(1, mockMemoryCache.CountSets);
9898
Assert.Equal(1, mockMemoryCache.CountGets);
9999
}
100+
101+
[Fact]
102+
public void BasicTest6DerivedClass()
103+
{
104+
using MockMemoryCache mockMemoryCache = MockMemoryCache.Default;
105+
106+
dynamic instance = TestHelpers.CreateInstance<DerivedTestClass2>(MemoryCacheBasicTests.TestResult.Assembly, mockMemoryCache);
107+
108+
dynamic result = instance.Add(1, 2);
109+
110+
Assert.Equal(3, result);
111+
Assert.Equal(1, mockMemoryCache.CountSets);
112+
Assert.Equal(1, mockMemoryCache.CountGets);
113+
}
100114
}
101115
}

0 commit comments

Comments
 (0)