So this next piece of the RoleProvider puzzle is a class which describes a custom property on our RoleProvider.

As you recall, RoleProvider lets us add new "roles" by simply adding items to a dictionary property on the class itself. What we want to do is turn each of those "roles" into properties on the class, which we can bind to using normal WinForms or WPF databinding.

So each of the roles is a bool property, representing whether the current user is a member of that particular role.

Here's the class:

public class RolePropertyDescriptor : PropertyDescriptor { public RolePropertyDescriptor(string name) : base(name, null) { } public override bool IsReadOnly { get { return true; } } public override void SetValue(object component, object value) { } public override bool CanResetValue(object component) { return false; } public override void ResetValue(object component) { } public override bool ShouldSerializeValue(object component) { return false; } public override Type ComponentType { get { return typeof(RoleProvider); } } public override Type PropertyType { get { return typeof(bool); } } public override object GetValue(object component) { RoleProvider rp = component as RoleProvider; if (rp == null) return null; return rp.IsInRole(Name); } } 

We inherit from the abstract PropertyDescriptor class, and override only the methods we have to. This is a read-only property, and (as you can see from the "PropertyType" override) it's of boolean type.

Note that the "GetValue" override calls back to our "IsInRole" method from the previous post, using the name of the current property, to determine if the user is in this role.

The next step is to define a class which tells the framework to add one of these properties to the RoleProvider class for each role we've defined. That's the next post.