RoleProvider - RoleTypeDescriptionProvider
Here's the last step in creating our RoleProvider class, which surfaces custom properties for roles you define so that WinForms and WPF can bind to it.
We've already defined a PropertyDescriptor to represent the boolean 'role' properties, and a CustomTypeDescriptor which tells the framework that these new properties exist. Now we need to tie that back to the original class, so that the RoleProvider class uses this new CustomTypeDescriptor to describe itself.
We do that with a TypeDescriptionProvider. Here's the code:
internal class RoleTypeDescriptionProvider : TypeDescriptionProvider { public RoleTypeDescriptionProvider() : this(TypeDescriptor.GetProvider(typeof(RoleProvider))) { } public RoleTypeDescriptionProvider(TypeDescriptionProvider parent) : base(parent) { } public override ICustomTypeDescriptor GetTypeDescriptor( Type objectType, object instance) { ICustomTypeDescriptor td = base.GetTypeDescriptor(objectType, instance); return new RoleCustomTypeDescriptor(td, instance); } }
We use this by adding an attribute to the top of our original class, like so:
[TypeDescriptionProvider(typeof(RoleTypeDescriptionProvider))] public class RoleProvider
And oila! Our class now has as many custom properties as we like, based on the roles we add to its "Roles" property!
Many thanks to the MSDN Managed Newsgroup Support Team for this blog post, which taught me how to do all this stuff, and to the commenters on that post who cleared up a few "gotchas" along the way.