Exemple #1
0
 protected References(ModuleWeaver moduleWeaver)
 {
     ModuleWeaver = moduleWeaver;
 }
        public static References Init(ModuleWeaver moduleWeaver)
        {
            if (moduleWeaver == null)
            {
                throw new ArgumentNullException(nameof(moduleWeaver));
            }

            References references = new References(moduleWeaver);

            TypeDefinition cacheAttributeType = moduleWeaver.FindTypeDefinition("SpatialFocus.MethodCache.CacheAttribute");

            references.CacheAttributeType = moduleWeaver.ModuleDefinition.ImportReference(cacheAttributeType);

            TypeDefinition noCacheAttributeType = moduleWeaver.FindTypeDefinition("SpatialFocus.MethodCache.NoCacheAttribute");

            references.NoCacheAttributeType = moduleWeaver.ModuleDefinition.ImportReference(noCacheAttributeType);

            TypeDefinition type = moduleWeaver.FindTypeDefinition(typeof(Type).FullName);

            references.TypeType = moduleWeaver.ModuleDefinition.ImportReference(type);

            TypeDefinition compilerGeneratedAttributeType = moduleWeaver.FindTypeDefinition(typeof(CompilerGeneratedAttribute).FullName);

            references.CompilerGeneratedAttributeType = moduleWeaver.ModuleDefinition.ImportReference(compilerGeneratedAttributeType);

            references.GetTypeFromHandleMethod =
                moduleWeaver.ModuleDefinition.ImportReference(type.Methods.Single(x => x.Name == nameof(Type.GetTypeFromHandle)));

            TypeDefinition timeSpanType = moduleWeaver.FindTypeDefinition(typeof(TimeSpan).FullName);

            references.TimeSpanType = moduleWeaver.ModuleDefinition.ImportReference(timeSpanType);
            references.TimeSpanFromSecondsMethod =
                moduleWeaver.ModuleDefinition.ImportReference(timeSpanType.Methods.Single(x => x.Name == nameof(TimeSpan.FromSeconds)));

            TypeDefinition nullableType = moduleWeaver.FindTypeDefinition(typeof(Nullable <>).FullName);

            references.NullableTimeSpanConstructor =
                moduleWeaver.ModuleDefinition.ImportReference(nullableType.GetConstructors()
                                                              .Single()
                                                              .MakeHostInstanceGeneric(references.TimeSpanType));

            TypeDefinition memoryCacheInterface = moduleWeaver.FindTypeDefinition("Microsoft.Extensions.Caching.Memory.IMemoryCache");

            references.MemoryCacheInterface = moduleWeaver.ModuleDefinition.ImportReference(memoryCacheInterface);

            TypeDefinition cacheExtensions = moduleWeaver.FindTypeDefinition("Microsoft.Extensions.Caching.Memory.CacheExtensions");

            references.CacheExtensionsType = moduleWeaver.ModuleDefinition.ImportReference(cacheExtensions);

            TypeDefinition memoryCacheEntryOptions =
                moduleWeaver.FindTypeDefinition("Microsoft.Extensions.Caching.Memory.MemoryCacheEntryOptions");

            references.MemoryCacheEntryOptionsType        = moduleWeaver.ModuleDefinition.ImportReference(memoryCacheEntryOptions);
            references.MemoryCacheEntryOptionsConstructor =
                moduleWeaver.ModuleDefinition.ImportReference(memoryCacheEntryOptions.GetConstructors().Single(x => !x.Parameters.Any()));
            references.MemoryCacheEntryOptionsAbsoluteExpirationRelativeToNowSetter =
                moduleWeaver.ModuleDefinition.ImportReference(memoryCacheEntryOptions.Properties
                                                              .Single(x => x.Name == "AbsoluteExpirationRelativeToNow")
                                                              .SetMethod);
            references.MemoryCacheEntryOptionsSlidingExpirationSetter =
                moduleWeaver.ModuleDefinition.ImportReference(memoryCacheEntryOptions.Properties.Single(x => x.Name == "SlidingExpiration")
                                                              .SetMethod);
            references.MemoryCacheEntryOptionsPrioritySetter =
                moduleWeaver.ModuleDefinition.ImportReference(
                    memoryCacheEntryOptions.Properties.Single(x => x.Name == "Priority").SetMethod);

            references.TryGetValueMethod =
                moduleWeaver.ModuleDefinition.ImportReference(cacheExtensions.Methods.Single(x => x.Name == "TryGetValue"));

            references.SetMethod =
                moduleWeaver.ModuleDefinition.ImportReference(cacheExtensions.Methods.Single(x =>
                                                                                             x.Name == "Set" && x.HasParameters && x.Parameters.Count == 3));

            references.SetMethodWithMemoryCacheEntryOptions = moduleWeaver.ModuleDefinition.ImportReference(
                cacheExtensions.Methods.Single(x =>
                                               x.Name == "Set" && x.HasParameters && x.Parameters.Count == 4 &&
                                               moduleWeaver.ModuleDefinition.ImportReference(x.Parameters.Last().ParameterType).Resolve() ==
                                               references.MemoryCacheEntryOptionsType.Resolve()));

            return(references);
        }