この質問(およびその回答) は、DataGridView をインターフェイス タイプに簡単にバインドして、基本インターフェイスから継承されたプロパティの列を取得できない理由を説明しています。
推奨される解決策は、カスタム TypeConverter を実装することです。私の試みは以下です。ただし、ICamel にバインドされた DataSource と DataGridView を作成しても、結果は 1 つの列 (ハンプ) になります。ICamel のどのプロパティを表示できるかを決定するために、私のコンバーターが .NET によって使用されているとは思いません。私は何を間違っていますか?
[TypeConverter(typeof(MyConverter))]
public interface IAnimal
{
string Name { get; set; }
int Legs { get; set; }
}
[TypeConverter(typeof(MyConverter))]
public interface ICamel : IAnimal
{
int Humps { get; set; }
}
public class MyConverter : TypeConverter
{
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
if(value is Type && (Type)value == typeof(ICamel))
{
List<PropertyDescriptor> propertyDescriptors = new List<PropertyDescriptor>();
foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(typeof(ICamel)))
{
propertyDescriptors.Add(pd);
}
foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(typeof(IAnimal)))
{
propertyDescriptors.Add(pd);
}
return new PropertyDescriptorCollection(propertyDescriptors.ToArray());
}
return base.GetProperties(context, value, attributes);
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
}