BindingList< T>其中T是实现其他接口的接口

本文关键字:其他接口 实现 接口 其中 BindingList | 更新日期: 2023-09-27 18:16:55

我坚持使用BindingList,其中T是扩展A接口的接口。当我在绑定中使用这个bindingList时,只有来自T的属性是可见的,而来自继承的A接口的属性则不可见。为什么会发生这种情况?看起来像是。net漏洞。这是我需要我的2个项目共享一些共同的功能。当PropertyChanged事件从baseImplementation隧接时,绑定列表的PropertyDescriptor为空。附加的接口和实现。设置方法

interface IExtendedInterface : IBaseInterface
{
    string C { get; }
}
interface IBaseInterface : INotifyPropertyChanged
{
    string A { get; }
    string B { get; }
}
public class BaseImplementation : IBaseInterface
{
    public string A
    {
        get { return "Base a"; }
    }
    public string B
    {
        get { return "base b"; }
        protected set
        {
            B = value;
            OnPropertyChanged("B");
        }
    }
    protected void OnPropertyChanged(string p)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(p));
    }
    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
}
public class ExtendedImplementation : BaseImplementation, IExtendedInterface
{
    public string C
    {
        get { return "Extended C"; }
    }
}
 private void SetupData()
    {
        BindingList<IExtendedInterface> list = new BindingList<IExtendedInterface>();
        list.Add(new ExtendedImplementation());
        list.Add(new ExtendedImplementation());
        dataGridView1.DataSource = list;
    }

BindingList< T>其中T是实现其他接口的接口

属性是通过(间接)TypeDescriptor.GetProperties(typeof(T))获得的,但其行为如预期的那样。来自接口的属性永远不会返回,即使是基于类的模型,除非它们位于该类型的公共API上(对于接口,这意味着位于直接类型上)。类继承是不同的,因为这些成员仍然在公共API上。当一个接口:ISomeOtherInterface时,它是"实现",而不是"继承"。举一个简单的例子,说明这可能是一个问题,考虑(完全合法的):

interface IA { int Foo {get;} }
interface IB { string Foo {get;} }
interface IC : IA, IB {}

;什么是IC.Foo

也许可以通过为接口注册一个自定义的TypeDescriptionProvider或者使用一个ITypedList来解决这个问题,但是这两种方法都很棘手。说实话,数据绑定在类上比在接口上更容易工作。