简体   繁体   English

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

[英]BindingList<T> where T is interface that implements other interface

I am stuck with BindingList where T is an interface that extends A interface. 我坚持使用BindingList,其中T是扩展A接口的接口。 WHen i use this bindingList in bindings, only properties from T are visible, while properties from inherited A interface are not. 当我在绑定中使用此bindingList时,只有来自T的属性是可见的,而来自继承的A接口的属性则不可见。 Why is it happening? 为什么会这样? It looks like a .net bug. 它看起来像.net错误。 This i required for my 2 projects to share some common functionality. 我需要为我的2个项目分享一些常用功能。 Also the binding List has PropertyDescriptor empty, when PropertyChanged event is tunneled from baseImplementation. 当PropertyChanged事件通过baseImplementation进行隧道传输时,绑定List的PropertyDescriptor为空。 Attached interfaces and implementations. 附加的接口和实现。 SetUp method in the end 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;
    }

The properties are obtained via (indirectly) TypeDescriptor.GetProperties(typeof(T)), but the behaviour is as expected. 属性是通过(间接)TypeDescriptor.GetProperties(typeof(T))获得的,但行为是预期的。 Properties from interfaces are never returned, even from class-based models, unless they are on the public API of that type (which for interfaces, means on the immediate type ). 即使是基于类的模型,也不会返回接口的属性,除非它们位于该类型的公共API上(对于接口,表示直接类型 )。 Class inheritance is different because those members are still on the public API. 类继承是不同的,因为这些成员仍然在公共API上。 When an interface : ISomeOtherInterface, that is "implements", not "inherits". 当一个接口:ISomeOtherInterface,即“implements”,而不是“inherits”。 To give a simple example of when this could be a problem, consider (fully legal): 举一个简单的例子说明这可能是一个问题,考虑(完全合法):

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

Now; 现在; what is IC.Foo ? 什么是IC.Foo?

You might be able to hack around this by registering a custom TypeDescriptionProvider for the interface, or using an ITypedList, but both of those are tricky. 可以通过为接口注册自定义TypeDescriptionProvider或使用ITypedList来解决此问题,但这两者都很棘手。 To be honest, data-binding simply works more easily with classes than interfaces. 说实话,数据绑定对于类而言比接口更容易。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 T实现接口的通用方法<T> - Generic method where T implements Interface<T> 通用方法,其中 T 是实现接口的列表 - Generic Method where T is List that implements interface 将DataGridView与BindingList一起使用 <T> 其中T是接口和跨线程更新 - Using DataGridView with a BindingList<T> where T is an interface and cross-thread updates 如何创建一个 List(of T),其中 T 是 (T) 的接口,List 的内容继承了一个实现 T 接口的抽象类 - How can I create a List(of T) where T is an interface of (T) and the contents of the List inherit an Abstract class that implements the interface of T 检查T是否实现接口,并在填充接口属性后返回T - Check if T implements an interface and return T after populating interface properties AdvancedCollectionView实现了ISupportIncrementalLoading接口,但是它不起作用 - AdvancedCollectionView implements the ISupportIncrementalLoading interface, but it doesn't work 检查“T”是否继承或实现了一个类/接口 - Check if 'T' inherits or implements a class/interface 检查对象是否使用通用接口实现接口 <T> 泛型类型是T的子代 - Checking if an object implements an interface with a generic <T> where the generic type is a child of T 为什么我不能将一个项目转换为泛型类型——其中泛型类型是一个接口,并且在检查该项目是否实现了所述接口之后? - Why can't I cast an item to a generic type — where the generic type is an interface, and after checking that the item implements said interface? 接口接口 <T> :T - interface Interface<T> : T
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM