public void TestGetOrAdd()
        {
            SynchronizedDictionary <int, string> data =
                new SynchronizedDictionary <int, string>(new Dictionary <int, string>());

            Assert.AreEqual("a", data.GetOrAdd(1, "a"));
            Assert.AreEqual("a", data.GetOrAdd(1, "b"));

            Assert.AreEqual("b", data.GetOrAdd(2, k => "b"));
            Assert.AreEqual("b", data.GetOrAdd(2, k => "c"));
        }
Пример #2
0
        private DictionaryAdapterMeta InternalGetAdapterMeta(
            Type type,
            PropertyDescriptor descriptor,
            DictionaryAdapterMeta other
            )
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (type.IsInterface == false)
            {
                throw new ArgumentException(
                          "Only interfaces can be adapted to a dictionary",
                          nameof(type)
                          );
            }

            return(interfaceToMeta.GetOrAdd(
                       type,
                       t =>
            {
                if (descriptor == null && other != null)
                {
                    descriptor = other.CreateDescriptor();
                }

                var typeBuilder = CreateTypeBuilder(type);
                return CreateAdapterMeta(type, typeBuilder, descriptor);
            }
                       ));
        }
Пример #3
0
        private DictionaryAdapterMeta InternalGetAdapterMeta(Type type,
                                                             PropertyDescriptor descriptor, DictionaryAdapterMeta other)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (type.GetTypeInfo().IsInterface == false)
            {
                throw new ArgumentException("Only interfaces can be adapted to a dictionary", "type");
            }

            return(interfaceToMeta.GetOrAdd(type, t =>
            {
                if (descriptor == null && other != null)
                {
                    descriptor = other.CreateDescriptor();
                }

#if FEATURE_LEGACY_REFLECTION_API
                var appDomain = Thread.GetDomain();
                var typeBuilder = CreateTypeBuilder(type, appDomain);
#else
                var typeBuilder = CreateTypeBuilder(type);
#endif
                return CreateAdapterMeta(type, typeBuilder, descriptor);
            }));
        }
Пример #4
0
 /// <summary>
 ///   Determines whether this assembly has internals visible to DynamicProxy.
 /// </summary>
 /// <param name="asm">The assembly to inspect.</param>
 internal static bool AreInternalsVisibleToDynamicProxy(Assembly asm)
 {
     return(internalsVisibleToDynamicProxy.GetOrAdd(asm, a =>
     {
         var internalsVisibleTo = asm.GetCustomAttributes <InternalsVisibleToAttribute>();
         return internalsVisibleTo.Any(attr => attr.AssemblyName.Contains(ModuleScope.DEFAULT_ASSEMBLY_NAME));
     }));
 }