/// <summary>
        /// Retrieves the appropriate custom attribute builder for a given attribute instance
        /// </summary>
        /// <param name="attributeType">The attribute type.  It cannot be null.</param>
        /// <returns>The custom attribute builder for it.</returns>
        private static ICustomAttributeBuilder GetCustomAttributeBuilder(Type attributeType)
        {
            if (attributeType == null)
            {
                throw new ArgumentNullException("attributeType");
            }

            ICustomAttributeBuilder cab = null;

            // We maintain a cache of known builder instances, created lazily
            if (KnownBuilders.TryGetValue(attributeType, out cab))
            {
                return(cab);
            }

            // Don't have a builder instance yet
            // See if we have a registered builder type for this attribute
            Type cabType = null;

            if (!KnownBuilderTypes.TryGetValue(attributeType, out cabType))
            {
                // Don't have an explicit builder -- see if we this attribute derives from
                // a known builder type and assume it is okay to use it.
                foreach (KeyValuePair <Type, Type> pair in KnownBuilderTypes)
                {
                    if (pair.Key.IsAssignableFrom(attributeType))
                    {
                        cabType = pair.Value;
                        break;
                    }
                }
            }

            // If don't have a builder -- see if the attribute is visible to the
            // client.  If so, we will attempt to build with our standard builder
            if (cabType == null)
            {
                cabType = typeof(StandardCustomAttributeBuilder);
            }

            // If we found a builder type, instantiate it now.  We'll reuse it
            cab = Activator.CreateInstance(cabType) as ICustomAttributeBuilder;

            // Don't cache null builders, because we may be re-using this cache for a next code-gen
            // run where we may have a different set of shared types.
            // TODO: We need to get rid of our static caches, because caches look different between
            //       builds of different DomainServices. E.g. for one DomainService an attribute may
            //       be shared, while in another it's not, and thus the KnownBuilders mapping is
            //       different between the two.
            //       Instead of static caches, we should consider storing state per build in a context
            //       object. The context object would not be shared against different builds of different
            //       DomainServices.
            if (cab != null)
            {
                KnownBuilders[attributeType] = cab;
            }

            return(cab);
        }
예제 #2
0
        /// <summary>
        /// Retrieves the appropriate custom attribute builder for a given attribute instance
        /// </summary>
        /// <param name="attributeType">The attribute type.  It cannot be null.</param>
        /// <returns>The custom attribute builder for it.</returns>
        private static ICustomAttributeBuilder GetCustomAttributeBuilder(Type attributeType)
        {
            if (attributeType == null)
            {
                throw new ArgumentNullException("attributeType");
            }

            ICustomAttributeBuilder cab = null;

            // We maintain a cache of known builder instances, created lazily
            if (KnownBuilders.TryGetValue(attributeType, out cab))
            {
                return(cab);
            }

            // Don't have a builder instance yet
            // See if we have a registered builder type for this attribute
            Type cabType = null;

            if (!KnownBuilderTypes.TryGetValue(attributeType, out cabType))
            {
                // Don't have an explicit builder -- see if we this attribute derives from
                // a known builder type and assume it is okay to use it.
                foreach (KeyValuePair <Type, Type> pair in KnownBuilderTypes)
                {
                    if (pair.Key.IsAssignableFrom(attributeType))
                    {
                        cabType = pair.Value;
                        break;
                    }
                }
            }

            // If don't have a builder -- see if the attribute is visible to the
            // client.  If so, we will attempt to build with our standard builder
            if (cabType == null)
            {
                cabType = typeof(StandardCustomAttributeBuilder);
            }

            // If we found a builder type, instantiate it now.  We'll reuse it
            if (cabType != null)
            {
                cab = Activator.CreateInstance(cabType) as ICustomAttributeBuilder;
            }

            // Don't cache null builders, because we may be re-using this cache for a next code-gen
            // run where we may have a different set of shared types.
            if (cab != null)
            {
                KnownBuilders[attributeType] = cab;
            }

            return(cab);
        }