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 HashSet <string> foreignKeyMembers = GetForeignKeyMembers(); // if any FK member of any association is also part of the primary key, then the key cannot be marked // Editable(false) bool keyIsEditable = false; foreach (PropertyDescriptor pd in _customTypeDescriptor.GetProperties()) { if (pd.Attributes[typeof(KeyAttribute)] != null && foreignKeyMembers.Contains(pd.Name)) { keyIsEditable = true; break; } } if (DataControllerTypeDescriptor.ShouldRegister(_customTypeDescriptor, keyIsEditable, foreignKeyMembers)) { // Extend the chain with one more descriptor. _customTypeDescriptor = new DataControllerTypeDescriptor(_customTypeDescriptor, keyIsEditable, foreignKeyMembers); } } return(_customTypeDescriptor); }
/// <summary> /// Gets the <see cref="TypeDescriptor"/> for the specified Type, using the specified parent descriptor /// as the base. Overrides should call base to ensure the <see cref="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="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"); } if (_parentProvider != null) { return(_parentProvider.GetTypeDescriptor(type, parent)); } return(parent); }