c# - BindingList<T> 其中 T 是实现其他接口(interface)的接口(interface)

标签 c# interface bindinglist inherited

我坚持使用 BindingList,其中 T 是一个扩展 A 接口(interface)的接口(interface)。当我在绑定(bind)中使用此 bindingList 时,只有来自 T 的属性可见,而来自继承的 A 接口(interface)的属性则不可见。为什么会这样?它看起来像一个 .net 错误。这是我的 2 个项目共享一些通用功能所必需的。当 PropertyChanged 事件从 baseImplementation 隧道传输时,绑定(bind)列表的 PropertyDescriptor 也为空。 附加的接口(interface)和实现。最后设置方法

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;
    }

最佳答案

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

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

现在; IC.Foo 是什么?

可能可以通过为接口(interface)注册自定义 TypeDescriptionProvider 或使用 ITypedList 来解决这个问题,但这两者都很棘手。老实说,与接口(interface)相比,数据绑定(bind)更容易与类一起工作。

关于c# - BindingList<T> 其中 T 是实现其他接口(interface)的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8374739/

相关文章:

c# - 从 BindingList 和 ISerializable 接口(interface)序列化派生类

c# - 哪种实现对于静态数据库连接更好?

c# - 更细粒度的 SQL 异常?

实现接口(interface)的枚举的 Java 参数和返回类型

java - 为什么不是所有的 Java 类都有接口(interface)?

c# - INotifyPropertyChanged 和 BindingList ListChanged 事件

wpf - 在 WPF 数据网格 MVVM 中创建倒数计时器列

c# - 对列表进行排序,但仍然能够访问索引和字段

c# - 为什么在 "try"或 "catch"或 "finally"的范围内声明的变量没有?

java - 使用字符串的多态性和创建对象