Exemple #1
0
        public void TestGetOrAddCacheHit(string key, DateTime value, string testKey, DateTime factoryValue)
        {
            Assume.That(key == testKey);
            Assume.That(factoryValue != value);

            // Arrange
            _sut.Add(key, value);
            Func <DateTime> factory = () => factoryValue;
            bool            cacheHit;

            // Act
            var result = _sut.GetOrAdd(testKey, factory, out cacheHit);

            // Assert
            Assert.AreEqual(value, result, "Correct result");
            Assert.IsTrue(cacheHit);
        }
Exemple #2
0
        private List <ObjectPropertyInfo> GetObjectPropertyInfos(Type type)
        {
            return(PropertyInfosCache.GetOrAdd(type, t =>
            {
#if NETSTANDARD11
                bool isDataContract = t.GetTypeInfo().GetCustomAttribute <DataContractAttribute>() != null;
                var infos = t.GetRuntimeProperties().Where(x => x.CanRead).ToArray();
#else
                bool isDataContract = t.GetCustomAttribute <DataContractAttribute>() != null;
                var infos = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);
#endif
                var res = new List <ObjectPropertyInfo>(infos.Length);

                foreach (var info in infos)
                {
                    if (info.GetCustomAttribute <IgnoreDataMemberAttribute>() != null)
                    {
                        continue;
                    }

                    DataMemberAttribute dataMemberAttr = null;
                    if (isDataContract)
                    {
                        dataMemberAttr = info.GetCustomAttribute <DataMemberAttribute>();
                        if (dataMemberAttr == null)
                        {
                            continue;
                        }
                    }

                    var mixpanelNameAttr = info.GetCustomAttribute <MixpanelNameAttribute>();
                    if (mixpanelNameAttr != null)
                    {
                        var isMixpanelNameEmpty = mixpanelNameAttr.Name.IsNullOrWhiteSpace();
                        res.Add(new ObjectPropertyInfo(
                                    isMixpanelNameEmpty ? info.Name : mixpanelNameAttr.Name,
                                    isMixpanelNameEmpty ? PropertyNameSource.Default : PropertyNameSource.MixpanelName,
                                    info));
                        continue;
                    }

                    if (dataMemberAttr != null)
                    {
                        var isDataMemberNameEmpty = dataMemberAttr.Name.IsNullOrWhiteSpace();
                        res.Add(new ObjectPropertyInfo(
                                    isDataMemberNameEmpty ? info.Name : dataMemberAttr.Name,
                                    isDataMemberNameEmpty ? PropertyNameSource.Default : PropertyNameSource.DataMember,
                                    info));
                        continue;
                    }

                    res.Add(new ObjectPropertyInfo(info.Name, PropertyNameSource.Default, info));
                }
                return res;
            }));
        }
        public static HalJsonTypeConfiguration GetConfigurationOrNull(Type t)
        {
            var value = Cache.GetOrAdd(t, Resolve);

            return(value == null ? null : value.Clone());
        }
Exemple #4
0
 public HalJsonTypeConfiguration GetOrCreateTypeConfiguration(Type type)
 {
     return(_config.GetOrAdd(type,
                             t => AttributeConfigurationResolver.GetConfigurationOrNull(type) ?? (HalJsonTypeConfiguration)Activator.CreateInstance(typeof(HalJsonTypeConfiguration <>).MakeGenericType(t))));
 }