Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public sealed class GroupNameAttribute : Attribute
- {
- public GroupNameAttribute(string groupName)
- {
- Guard.ArgumentNotNull(groupName, "groupName");
- this.GroupName = groupName;
- }
- public string GroupName { get; private set; }
- public static void Apply(INotifyPropertyChanged target)
- {
- Guard.ArgumentNotNull(target, "target");
- Func<PropertyInfo, bool> isBool = property =>
- {
- if(property.PropertyType != typeof(bool))
- {
- throw new ArgumentException(
- string.Format("GroupNameAttribute can be applied only to {0}.", typeof(bool).FullName));
- }
- return true;
- };
- var groups = (from property in target.GetType().GetProperties()
- from attribute in (GroupNameAttribute[])GetCustomAttributes(property, typeof(GroupNameAttribute))
- where isBool(property)
- select new { property, attribute.GroupName }).GroupBy(el => el.GroupName);
- foreach(var group in groups)
- {
- var properties = group.Select(g => g.property).ToArray();
- foreach(var property in properties)
- {
- var currentProperty = property;
- target.PropertyChanged += (sender, e) =>
- {
- if(e.PropertyName != currentProperty.Name) return;
- var newValue = (bool)currentProperty.GetValue(target, null);
- if(!newValue) return;
- foreach(var otherProperty in properties)
- {
- if(otherProperty == currentProperty) continue;
- otherProperty.SetValue(target, false, null);
- }
- };
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement