Suppose we have two classes A and B. Class A has some browsable and non-browsable properties, class B is derived from A and implements ICustomTypeDescriptor interface (simply through the calls to the TypeDescriptor methods with true passed as noCustomTypeDesc parameter). Here is the code:

public class A
{
  public int BrowsableProperty { get; set; }

  [Browsable(false)]
  public int NonBrowsableProperty { get; set; }
}

public class B : A, ICustomTypeDescriptor
{
  public AttributeCollection GetAttributes() => TypeDescriptor.GetAttributes(this, true);
  public string GetClassName() => TypeDescriptor.GetClassName(this, true);
  public string GetComponentName() => TypeDescriptor.GetComponentName(this, true);
  public TypeConverter GetConverter() => TypeDescriptor.GetConverter(this, true);
  public EventDescriptor GetDefaultEvent() => TypeDescriptor.GetDefaultEvent(this, true);
  public PropertyDescriptor GetDefaultProperty() => TypeDescriptor.GetDefaultProperty(this, true);
  public object GetEditor(Type editorBaseType) => TypeDescriptor.GetEditor(this, editorBaseType, true);
  public EventDescriptorCollection GetEvents() => TypeDescriptor.GetEvents(this, true);
  public EventDescriptorCollection GetEvents(Attribute[] attributes) => TypeDescriptor.GetEvents(this, attributes, true);
  public PropertyDescriptorCollection GetProperties() => TypeDescriptor.GetProperties(this, true);
  public PropertyDescriptorCollection GetProperties(Attribute[] attributes) => TypeDescriptor.GetProperties(this, attributes, true);
  public object GetPropertyOwner(PropertyDescriptor pd) => this;
}

Now let's see what TypeDescriptor.GetProperties returns for the instances of these classes:

var a = new A();
var b = new B();
var browsable = new Attribute[] { BrowsableAttribute.Yes };

Console.WriteLine("Properties of A:");
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(a, browsable, true))
  Console.WriteLine(property.Name);
Console.WriteLine();

Console.WriteLine("Properties of B:");
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(b, browsable, true))
  Console.WriteLine(property.Name);

Note that noCustomTypeDesc = true is intentional here. This is just a demonstration of what it looks like inside the ICustomTypeDescriptor implementation, where it is invoked with the same parameters. Strangely, this results in the following output:

Properties of A:
BrowsableProperty

Properties of B:
BrowsableProperty
NonBrowsableProperty

Why?

0

There are 0 best solutions below