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

use*_*952 4 c# interface bindinglist inherited

我坚持使用BindingList,其中T是扩展A接口的接口.当我在绑定中使用此bindingList时,只有来自T的属性是可见的,而来自继承的A接口的属性则不可见.为什么会这样?它看起来像.net错误.我需要为我的2个项目分享一些常用功能.当PropertyChanged事件通过baseImplementation进行隧道传输时,绑定List的PropertyDescriptor为空.附加的接口和实现.SetUp方法到底

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;
    }
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 6

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

interface IA { int Foo {get;} }
interface IB { string Foo {get;} }
interface IC : IA, IB {}
Run Code Online (Sandbox Code Playgroud)

现在; 什么是IC.Foo?

可以通过为接口注册自定义TypeDescriptionProvider或使用ITypedList来解决此问题,但这两者都很棘手.说实话,数据绑定对于类而言比接口更容易.