Exemplo n.º 1
0
        /// <summary>
        /// Gets the <see cref="System.ComponentModel.TypeDescriptor"/> for the specified Type, using the specified parent descriptor
        /// as the base. Overrides should call base to ensure the <see cref="System.ComponentModel.TypeDescriptor"/>s are chained properly.
        /// </summary>
        /// <param name="type">The Type to return a descriptor for.</param>
        /// <param name="parent">The parent descriptor.</param>
        /// <returns>The <see cref="System.ComponentModel.TypeDescriptor"/> for the specified Type.</returns>
        public virtual ICustomTypeDescriptor GetTypeDescriptor(Type type, ICustomTypeDescriptor parent)
        {
            if (type == null)
            {
                throw Error.ArgumentNull("type");
            }
            if (parent == null)
            {
                throw Error.ArgumentNull("parent");
            }

            return(_parentProvider != null?_parentProvider.GetTypeDescriptor(type, parent) : parent);
        }
Exemplo n.º 2
0
        public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
        {
            if (objectType == null && instance != null)
            {
                objectType = instance.GetType();
            }

            if (_type != objectType)
            {
                // In inheritance scenarios, we might be called to provide a descriptor
                // for a derived Type. In that case, we just return base.
                return(base.GetTypeDescriptor(objectType, instance));
            }

            if (_customTypeDescriptor == null)
            {
                // CLR, buddy class type descriptors
                _customTypeDescriptor = base.GetTypeDescriptor(objectType, instance);

                // EF, any other custom type descriptors provided through MetadataProviders.
                _customTypeDescriptor = _metadataProvider.GetTypeDescriptor(objectType, _customTypeDescriptor);

                // initialize FK members AFTER our type descriptors have chained
                var foreignKeyMembers = GetForeignKeyMembers();

                // if any FK member of any association is also part of the primary key, then the key cannot be marked
                // Editable(false)
                var keyIsEditable = _customTypeDescriptor.GetProperties()
                                    .Cast <PropertyDescriptor>()
                                    .Where(pd => pd.Attributes[typeof(KeyAttribute)] != null)
                                    .Any(pd => foreignKeyMembers.Contains(pd.Name));

                if (DataControllerTypeDescriptor.ShouldRegister(_customTypeDescriptor, keyIsEditable, foreignKeyMembers))
                {
                    // Extend the chain with one more descriptor.
                    _customTypeDescriptor = new DataControllerTypeDescriptor(_customTypeDescriptor, keyIsEditable, foreignKeyMembers);
                }
            }

            return(_customTypeDescriptor);
        }