public PropertyDescriptorCollection GetProperties() { var meta = _dynamic.GetMetaObject(Expression.Constant(_dynamic)); var memberNames = meta.GetDynamicMemberNames(); var props = new PropertyDescriptorCollection(new PropertyDescriptor[] { }); foreach (var memberName in memberNames) { if (!properties.ContainsKey(memberName)) { var newProperty = new DynamicPropertyDescriptor(_dynamic, memberName); properties.Add(memberName, newProperty); } props.Add(properties[memberName]); } if (InstanceType != null) { foreach (var propertyInfo in InstancePropertyInfo) { props.Add(InstancePropertyDescriptionCollection[propertyInfo.Name]); } } return(props); }
public void AddAttribute(string propertyName, Attribute attribute) { if (!ContainsProperty(propertyName)) { throw new Exception("Property doesn't exist"); } DynamicPropertyDescriptor property = null; if (properties.ContainsKey(propertyName)) { property = properties[propertyName]; properties.Remove(propertyName); } else { property = new DynamicPropertyDescriptor(new ExpandoObject(), propertyName); } var newPropertyWithAttribute = new DynamicPropertyDescriptor(property, new Attribute[] { attribute }); properties.Add(propertyName, newPropertyWithAttribute); }
public bool RemoveAttribute <TAttribute>(string propertyName) where TAttribute : Attribute { if (!ContainsProperty(propertyName)) { throw new Exception("Property doesn't exist"); } else if (InstancePropertyDescriptionCollection.ContainsKey(propertyName) && InstancePropertyDescriptionCollection[propertyName].Attributes.OfType <TAttribute>().Count() > 0) { throw new Exception("Can't remove attribute from strongly typed instance."); } if (properties.ContainsKey(propertyName)) { DynamicPropertyDescriptor property = properties[propertyName]; properties.Remove(propertyName); var attributesToCopy = new List <Attribute>(); for (int i = 0; i < property.Attributes.Count; i++) { if (property.Attributes[i].GetType() != typeof(TAttribute)) { attributesToCopy.Add(property.Attributes[i]); } } var newPropertyWithAttribute = new DynamicPropertyDescriptor(_dynamic, propertyName); foreach (var attribute in attributesToCopy) { newPropertyWithAttribute = new DynamicPropertyDescriptor(newPropertyWithAttribute, new Attribute[] { attribute }); } properties.Add(propertyName, newPropertyWithAttribute); return(true); } return(false); }
public override bool Equals(object obj) { DynamicPropertyDescriptor other = obj as DynamicPropertyDescriptor; return(other != null && other._propertyName.Equals(_propertyName)); }
public DynamicPropertyDescriptor(DynamicPropertyDescriptor oldDynamicPropertyDescriptor, Attribute[] newAttrs) : base(oldDynamicPropertyDescriptor, newAttrs) { _propertyName = oldDynamicPropertyDescriptor.Name; _owner = oldDynamicPropertyDescriptor._owner; }