What about the following code?
(not fully trialed out yet so be gentle ;-)
public class CslaTypeDescriptorProvider<T> : System.ComponentModel.TypeDescriptionProvider{
private static System.ComponentModel.TypeDescriptionProvider _baseProvider = System.ComponentModel.TypeDescriptor.GetProvider(typeof(T)); public CslaTypeDescriptorProvider():base(_baseProvider){ }
public override System.ComponentModel.ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance){
System.ComponentModel.
ICustomTypeDescriptor defaultDescriptor = base.GetTypeDescriptor(objectType, instance); return instance == null ? defaultDescriptor : new CslaTypeDescriptor(defaultDescriptor, instance);}
}
public class CslaTypeDescriptor : System.ComponentModel.CustomTypeDescriptor{
public CslaTypeDescriptor(System.ComponentModel.ICustomTypeDescriptor parent, object instance):base(parent){
}
public override System.ComponentModel.PropertyDescriptorCollection GetProperties(){
return GetProperties(null);}
public override System.ComponentModel.PropertyDescriptorCollection GetProperties(Attribute[] attributes){
System.ComponentModel.
PropertyDescriptorCollection col = new System.ComponentModel.PropertyDescriptorCollection(null); foreach (System.ComponentModel.PropertyDescriptor p in base.GetProperties(attributes)){
col.Add(
new Core.PropertyDescriptor(p));}
return col;}
}
public class PropertyDescriptor : System.ComponentModel.PropertyDescriptor
{
static Dictionary<Type, List<Csla.Core.IPropertyInfo>> _propertyTracker = null; static Dictionary<Type, List<Csla.Core.IPropertyInfo>> PropertyTracker{
get{
if (_propertyTracker == null){
_propertyTracker =
new Dictionary<Type, List<Csla.Core.IPropertyInfo>>();}
return _propertyTracker;}
}
System.ComponentModel.
PropertyDescriptor _descriptor = null; public PropertyDescriptor(System.ComponentModel.PropertyDescriptor descriptor):base(descriptor){
_descriptor = descriptor;
SetDisplayName();
}
public PropertyDescriptor(System.ComponentModel.PropertyDescriptor descriptor, System.Attribute[] attrs):
base(descriptor, attrs){
_descriptor = descriptor;
SetDisplayName();
}
private void SetDisplayName(){
List<Csla.Core.IPropertyInfo> list = null; if (PropertyTracker.ContainsKey(_descriptor.ComponentType)){
list = PropertyTracker[_descriptor.ComponentType];
}
else{
list =
new List<Csla.Core.IPropertyInfo>();System.Reflection.
FieldInfo[] fi = _descriptor.ComponentType.GetFields(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.FlattenHierarchy); if (fi != null && fi.Length > 0){
foreach (System.Reflection.FieldInfo f in fi){
Csla.Core.
IPropertyInfo propInfo = f.GetValue(null) as Csla.Core.IPropertyInfo; if (propInfo != null) list.Add(propInfo);}
}
}
foreach (Csla.Core.IPropertyInfo propInfo in list){
if (propInfo.Name == _descriptor.Name){
this._displayName = propInfo.FriendlyName; break;}
}
}
public override bool CanResetValue(object component){
return _descriptor.CanResetValue(component);}
public override Type ComponentType{
get { return _descriptor.ComponentType; }}
public override object GetValue(object component){
return _descriptor.GetValue(component);}
public override bool IsReadOnly{
get { return _descriptor.IsReadOnly; }}
public override Type PropertyType{
get { return _descriptor.PropertyType; }}
public override void ResetValue(object component){
_descriptor.ResetValue(component);
}
public override void SetValue(object component, object value){
_descriptor.SetValue(component, value);
}
public override bool ShouldSerializeValue(object component){
return _descriptor.ShouldSerializeValue(component);}
string _displayName = string.Empty; public override string DisplayName{
get{
if (string.IsNullOrEmpty(_displayName)){
return base.DisplayName;}
else{
return _displayName;}
}
}
}
////// I have my own base classes that sit between CSLA and my concrete classes. I tagged them with the attribute:
[
Serializable][System.ComponentModel.
TypeDescriptionProvider(typeof(Core.CslaTypeDescriptorProvider<Csla.Core.BindableBase>))] public abstract class ReadOnlyBase<T> : Csla.ReadOnlyBase<T> where T : ReadOnlyBase<T>{
}
[
Serializable][System.ComponentModel.
TypeDescriptionProvider(typeof(Core.CslaTypeDescriptorProvider<Csla.Core.BusinessBase>))] public abstract class BusinessBase<T> : Csla.BusinessBase<T>, IBrokenRules where T : Csla.BusinessBase<T>{
}
Copyright (c) Marimer LLC