public void HybridTableOptimizationHandsOffAllValuesToDictionary(int count)
        {
            KeyedMemoizer<int?, int> memoizer = new KeyedMemoizer<int?, int>();
            for (int i = 0; i < count; i++)
                Assert.AreEqual(i, memoizer.Memoize(i, () => i));

            for (int i = 0; i < count; i++)
                Assert.AreEqual(i, memoizer.Memoize(i, () => { throw new InvalidOperationException("Should not happen."); }));
        }
Example #2
0
        public void HybridTableOptimizationHandsOffAllValuesToDictionary(int count)
        {
            KeyedMemoizer <int?, int> memoizer = new KeyedMemoizer <int?, int>();

            for (int i = 0; i < count; i++)
            {
                Assert.AreEqual(i, memoizer.Memoize(i, () => i));
            }

            for (int i = 0; i < count; i++)
            {
                Assert.AreEqual(i, memoizer.Memoize(i, () => { throw new InvalidOperationException("Should not happen."); }));
            }
        }
Example #3
0
        internal AttributeUsageAttribute GetAttributeUsage(ITypeInfo attributeType)
        {
            return(attributeUsageMemoizer.Memoize(attributeType, () =>
            {
                if (attributeType.FullName == @"System.AttributeUsageAttribute")
                {
                    // Note: Avoid indefinite recursion when determining whether AttributeUsageAttribute itself is inheritable.
                    return new AttributeUsageAttribute(AttributeTargets.Class)
                    {
                        Inherited = true
                    };
                }

                Type resolvedAttributeType = attributeType.Resolve(false);
                if (!Reflector.IsUnresolved(resolvedAttributeType))
                {
                    // We use the resolved type when possible primarily because ReSharper's Metadata reflection
                    // policy cannot resolve types in System and mscorlib which causes problems ironically when
                    // trying to resolve AttributeUsageAttribute itself.  However this is also a useful optimization
                    // in the case where the type can be resolved. -- Jeff.
                    var attributeUsages = (AttributeUsageAttribute[])resolvedAttributeType.GetCustomAttributes(
                        typeof(AttributeUsageAttribute), true);

                    if (attributeUsages.Length != 0)
                    {
                        return attributeUsages[0];
                    }
                }
                else
                {
                    try
                    {
                        var attributeUsage = AttributeUtils.GetAttribute <AttributeUsageAttribute>(attributeType, true);
                        if (attributeUsage != null)
                        {
                            return attributeUsage;
                        }
                    }
                    catch (TargetParameterCountException)
                    {
                        // This is a hack to work around the fact that ReSharper does not correctly handle
                        // attribute parameters with enum values.  In particular, this affects the first
                        // parameter of AttributeUsageAttribute. -- Jeff.
                        return new AttributeUsageAttribute(AttributeTargets.All)
                        {
                            Inherited = true,
                            AllowMultiple = true
                        };
                    }
                }

                return new AttributeUsageAttribute(AttributeTargets.All)
                {
                    Inherited = true
                };
            }));
        }
Example #4
0
        /// <inheritdoc />
        public ITypeInfo GetType(string typeName)
        {
            if (typeName == null)
            {
                throw new ArgumentNullException("typeName");
            }

            return(getTypeMemoizer.Memoize(typeName, () =>
                                           ReflectionPolicy.GetAssemblyType(this, typeName)));
        }
        public void WhenValueNotPresentPopulatesAndMemoizesItByKey(int initialSize)
        {
            KeyedMemoizer<string, int> memoizer = new KeyedMemoizer<string, int>();

            for (int i = 0; i < initialSize; i++)
                Assert.AreEqual(i, memoizer.Memoize(i + "x", () => i));

            Assert.AreEqual(42, memoizer.Memoize("42", () => 42));
            Assert.AreEqual(42, memoizer.Memoize("42", () => { throw new InvalidOperationException("Should not be called"); }));

            Assert.AreEqual(31, memoizer.Memoize("31", () => 31));
            Assert.AreEqual(31, memoizer.Memoize("31", () => { throw new InvalidOperationException("Should not be called"); }));

            Assert.AreEqual(-1, memoizer.Memoize(null, () => -1));

            Assert.AreEqual(42, memoizer.Memoize("42", () => { throw new InvalidOperationException("Should not be called"); }));
            Assert.AreEqual(-1, memoizer.Memoize(null, () => { throw new InvalidOperationException("Should not be called"); }));
            Assert.AreEqual(31, memoizer.Memoize("31", () => { throw new InvalidOperationException("Should not be called"); }));
        }
Example #6
0
        public void WhenValueNotPresentPopulatesAndMemoizesItByKey(int initialSize)
        {
            KeyedMemoizer <string, int> memoizer = new KeyedMemoizer <string, int>();

            for (int i = 0; i < initialSize; i++)
            {
                Assert.AreEqual(i, memoizer.Memoize(i + "x", () => i));
            }

            Assert.AreEqual(42, memoizer.Memoize("42", () => 42));
            Assert.AreEqual(42, memoizer.Memoize("42", () => { throw new InvalidOperationException("Should not be called"); }));

            Assert.AreEqual(31, memoizer.Memoize("31", () => 31));
            Assert.AreEqual(31, memoizer.Memoize("31", () => { throw new InvalidOperationException("Should not be called"); }));

            Assert.AreEqual(-1, memoizer.Memoize(null, () => - 1));

            Assert.AreEqual(42, memoizer.Memoize("42", () => { throw new InvalidOperationException("Should not be called"); }));
            Assert.AreEqual(-1, memoizer.Memoize(null, () => { throw new InvalidOperationException("Should not be called"); }));
            Assert.AreEqual(31, memoizer.Memoize("31", () => { throw new InvalidOperationException("Should not be called"); }));
        }
Example #7
0
 /// <inheritdoc />
 public Assembly Resolve(bool throwOnError)
 {
     return(resolveMemoizer.Memoize(throwOnError, () =>
                                    ReflectorResolveUtils.ResolveAssembly(this, true, throwOnError)));
 }
Example #8
0
 /// <inheritdoc />
 public EventInfo Resolve(bool throwOnError)
 {
     return(resolveMemoizer.Memoize(throwOnError,
                                    () => ReflectorResolveUtils.ResolveEvent(this, throwOnError)));
 }
 /// <inheritdoc />
 public object Resolve(bool throwOnError)
 {
     return(resolveMemoizer.Memoize(throwOnError, () =>
                                    ReflectorAttributeUtils.CreateAttribute(this, throwOnError)));
 }
 public TestCase GetTestCase(TestData testData)
 {
     return(testCases.Memoize(testData.Id, () => testCaseFactory.GetTestCase(testData)));
 }
Example #11
0
 /// <inheritdoc />
 public Type Resolve(bool throwOnError)
 {
     return(resolveMemoizer.Memoize(throwOnError, () => Resolve(null, throwOnError)));
 }