Beispiel #1
0
        public IEnumerable <TAttribute> GetAttribute <TAttribute>(string propertyName) where TAttribute : Attribute
        {
            if (!ContainsProperty(propertyName))
            {
                throw new Exception("Property doesn't exist");
            }

            if (InstancePropertyDescriptionCollection != null & InstancePropertyDescriptionCollection.ContainsKey(propertyName))
            {
                var property   = GetProperties()[propertyName];
                var collection = property.Attributes.OfType <TAttribute>().ToList();

                if (properties.ContainsKey(propertyName))
                {
                    var dynamicProperty = properties[propertyName];
                    collection.AddRange(dynamicProperty.Attributes.OfType <TAttribute>());
                }

                return(collection);
            }
            else
            {
                var property = GetProperties()[propertyName];
                return(property.Attributes.OfType <TAttribute>());
            }
        }
Beispiel #2
0
 private object GetMember(string propertyName)
 {
     if (Instance != null && InstancePropertyDescriptionCollection.ContainsKey(propertyName))
     {
         return(GetProperties().Find(propertyName, true).GetValue(Instance));
     }
     else
     {
         return(GetProperties().Find(propertyName, true).GetValue(_dynamic));
     }
 }
Beispiel #3
0
        private void SetMember(string propertyName, object value)
        {
            var property = GetProperties().Find(propertyName, true);

            if (Instance != null && InstancePropertyDescriptionCollection.ContainsKey(propertyName))
            {
                property.SetValue(Instance, value);
            }
            else
            {
                property.SetValue(_dynamic, value);
            }
        }
Beispiel #4
0
        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);
        }