Esempio n. 1
0
        public static MetadataViewFactory GetMetadataViewFactory(Type viewType)
        {
            Assumes.NotNull(viewType);
            Assumes.IsTrue(viewType.IsInterface);

            MetadataViewFactory metadataViewFactory;
            bool foundMetadataViewFactory;

            using (new ReadLock(_lock))
            {
                foundMetadataViewFactory = _metadataViewFactories.TryGetValue(viewType, out metadataViewFactory);
            }

            // No factory exists
            if (!foundMetadataViewFactory)
            {
                // Try again under a write lock if still none generate the proxy
                Type generatedProxyType = GenerateInterfaceViewProxyType(viewType);
                Assumes.NotNull(generatedProxyType);

                MetadataViewFactory generatedMetadataViewFactory = (MetadataViewFactory)Delegate.CreateDelegate(
                    typeof(MetadataViewFactory), generatedProxyType.GetMethod(MetadataViewGenerator.MetadataViewFactoryName, BindingFlags.Public | BindingFlags.Static));
                Assumes.NotNull(generatedMetadataViewFactory);

                using (new WriteLock(_lock))
                {
                    if (!_metadataViewFactories.TryGetValue(viewType, out metadataViewFactory))
                    {
                        metadataViewFactory = generatedMetadataViewFactory;
                        _metadataViewFactories.Add(viewType, metadataViewFactory);
                    }
                }
            }
            return(metadataViewFactory);
        }
Esempio n. 2
0
 public static TMetadataView CreateMetadataView <TMetadataView>(MetadataViewFactory metadataViewFactory, IDictionary <string, object> metadata)
 {
     Assumes.NotNull(metadataViewFactory);
     // we are simulating the Activator.CreateInstance behavior by wrapping everything in a TargetInvocationException
     try
     {
         return((TMetadataView)metadataViewFactory.Invoke(metadata));
     }
     catch (Exception e)
     {
         throw new TargetInvocationException(e);
     }
 }
        public static MetadataViewFactory GetMetadataViewFactory(Type viewType)
        {
            if (viewType == null)
            {
                throw new ArgumentNullException(nameof(viewType));
            }

            if (!viewType.IsInterface)
            {
                throw new Exception(SR.Diagnostic_InternalExceptionMessage);
            }

            MetadataViewFactory metadataViewFactory;
            bool foundMetadataViewFactory;

            using (new ReadLock(_lock))
            {
                foundMetadataViewFactory = _metadataViewFactories.TryGetValue(viewType, out metadataViewFactory);
            }

            // No factory exists
            if (!foundMetadataViewFactory)
            {
                // Try again under a write lock if still none generate the proxy
                Type generatedProxyType = GenerateInterfaceViewProxyType(viewType);
                if (generatedProxyType == null)
                {
                    throw new Exception(SR.Diagnostic_InternalExceptionMessage);
                }

                MetadataViewFactory generatedMetadataViewFactory = (MetadataViewFactory)Delegate.CreateDelegate(
                    typeof(MetadataViewFactory), generatedProxyType.GetMethod(MetadataViewGenerator.MetadataViewFactoryName, BindingFlags.Public | BindingFlags.Static));
                if (generatedMetadataViewFactory == null)
                {
                    throw new Exception(SR.Diagnostic_InternalExceptionMessage);
                }

                using (new WriteLock(_lock))
                {
                    if (!_metadataViewFactories.TryGetValue(viewType, out metadataViewFactory))
                    {
                        metadataViewFactory = generatedMetadataViewFactory;
                        _metadataViewFactories.Add(viewType, metadataViewFactory);
                    }
                }
            }
            return(metadataViewFactory);
        }