Skip to content

Commit

Permalink
1.0.6
Browse files Browse the repository at this point in the history
fix reference problem
  • Loading branch information
Yvees committed Jan 25, 2018
1 parent 497c6e1 commit 36ba930
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Injects method cache code.
### This is an add-in for [Fody](https://github.com/Fody/Fody/)
[Introduction to Fody](http://github.com/Fody/Fody/wiki/SampleUsage)

This project was forked by [MethodTimer](https://github.com/Fody/MethodTimer) original.
This project was forked by [MethodCache](https://github.com/Dresel/MethodCache) original.
And updated to support DotNet Core.

## Milestone
Expand Down
87 changes: 87 additions & 0 deletions src/Cache.Fody/AssemblyResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using Mono.Cecil;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cache.Fody
{
public class AssemblyResolver : IAssemblyResolver
{
Dictionary<string, string> referenceDictionary;
List<string> splitReferences;
Dictionary<string, AssemblyDefinition> assemblyDefinitionCache = new Dictionary<string, AssemblyDefinition>(StringComparer.InvariantCultureIgnoreCase);

public AssemblyResolver()
{
}

public AssemblyResolver(List<string> splitReferences)
{
referenceDictionary = new Dictionary<string, string>();
this.splitReferences = splitReferences;

foreach (var filePath in splitReferences)
{
referenceDictionary[Path.GetFileNameWithoutExtension(filePath)] = filePath;
}
}

AssemblyDefinition GetAssembly(string file, ReaderParameters parameters)
{
if (assemblyDefinitionCache.TryGetValue(file, out var assembly))
{
return assembly;
}
if (parameters.AssemblyResolver == null)
{
parameters.AssemblyResolver = this;
}
try
{
return assemblyDefinitionCache[file] = AssemblyDefinition.ReadAssembly(file, parameters);
}
catch (Exception exception)
{
throw new Exception($"Could not read '{file}'.", exception);
}
}

public AssemblyDefinition Resolve(string assemblyName)
{
return Resolve(new AssemblyNameReference(assemblyName, null));
}

public AssemblyDefinition Resolve(AssemblyNameReference assemblyNameReference)
{
return Resolve(assemblyNameReference, new ReaderParameters());
}

public AssemblyDefinition Resolve(AssemblyNameReference assemblyNameReference, ReaderParameters parameters)
{
if (parameters == null)
{
parameters = new ReaderParameters();
}

if (referenceDictionary.TryGetValue(assemblyNameReference.Name, out var fileFromDerivedReferences))
{
return GetAssembly(fileFromDerivedReferences, parameters);
}

var joinedReferences = string.Join(Environment.NewLine, splitReferences.OrderBy(x => x));
return null;
}

public virtual void Dispose()
{
foreach (var value in assemblyDefinitionCache.Values)
{
value?.Dispose();
}
}

}
}
2 changes: 1 addition & 1 deletion src/Cache.Fody/ModuleWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Mono.Cecil.Rocks;
using Mono.Cecil.Cil;
using Fody;
using System;

namespace Cache.Fody
{
Expand Down Expand Up @@ -48,6 +49,5 @@ public override IEnumerable<string> GetAssembliesForScanning()
yield return "System.Core";
yield return "netstandard";
}

}
}
18 changes: 16 additions & 2 deletions src/Cache.Fody/ReferenceFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
using global::Fody;
using Mono.Cecil;
using Mono.Cecil.Rocks;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

public static class ReferenceFinder
{
//private static IAssemblyResolver AssemblyResolver { get; set; }
private static AssemblyResolver AssemblyResolver { get; set; }

//private static TypeDefinition CompilerGeneratedAttribute { get; set; }

Expand Down Expand Up @@ -36,7 +39,18 @@ public static void LoadReferences(BaseModuleWeaver weaver)
DictionaryConstructor = weaver.FindType("Dictionary`2").Resolve().GetConstructors().FirstOrDefault();
DictionaryAddMethod = weaver.FindType("Dictionary`2").Method("Add");
SystemTypeGetTypeFromHandleMethod = weaver.FindType("Type").Method("GetTypeFromHandle");
CacheAssembly = new DefaultAssemblyResolver().Resolve(weaver.ModuleDefinition.AssemblyReferences.FirstOrDefault(r => r.Name == "Cache"));

//load Cache reference
var references = SplitUpReferences(weaver);
AssemblyResolver = new AssemblyResolver(references);
CacheAssembly = AssemblyResolver.Resolve("Cache");
}

private static List<string> SplitUpReferences(BaseModuleWeaver weaver)
{
return weaver.References
.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.ToList();
}

}
Expand Down
7 changes: 6 additions & 1 deletion src/Cache.Fody/WeaveHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -443,6 +444,11 @@ public static void WeaveMethod(BaseModuleWeaver weaver, MethodDefinition methodD
propertyGet = propertyInject.GetMethod;
}

if (propertyInject == null )
{
propertyInject = methodDefinition.DeclaringType.Properties.FirstOrDefault(p => p.Name == "Cache");
}

//weave mthod
methodDefinition.Body.InitLocals = true;

Expand Down Expand Up @@ -490,7 +496,6 @@ public static void WeaveMethod(BaseModuleWeaver weaver, MethodDefinition methodD
current = current.Append(processor.Create(OpCodes.Call, methodDefinition.Module.ImportReference(refSetter)), processor);
}


current = InjectCacheKeyCreatedCode(weaver, methodDefinition, current, processor, cacheKeyIndex);

TypeDefinition propertyGetReturnTypeDefinition = propertyGet.ReturnType.Resolve();
Expand Down
8 changes: 4 additions & 4 deletions src/Cache/Cache.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

<PropertyGroup>
<TargetFrameworks>net46;netstandard2.0</TargetFrameworks>
<PackageVersion>1.0.4</PackageVersion>
<PackageVersion>1.0.6</PackageVersion>
<Description>Injects method cache code.</Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/KevinYeti/Cache</PackageProjectUrl>
<PackageIconUrl>https://raw.githubusercontent.com/KevinYeti/Cache/master/icon.png</PackageIconUrl>
<PackageTags>Cache, ILWeaving, Fody, Cecil, AOP</PackageTags>
<PackageLicenseUrl>https://github.com/KevinYeti/Cache/blob/master/LICENSE</PackageLicenseUrl>
<PackageId>Cache.Fody</PackageId>
<AssemblyVersion>1.0.4.0</AssemblyVersion>
<FileVersion>1.0.4.0</FileVersion>
<Version>1.0.4</Version>
<AssemblyVersion>1.0.6.0</AssemblyVersion>
<FileVersion>1.0.6.0</FileVersion>
<Version>1.0.6</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 36ba930

Please sign in to comment.