BindingList <T>, where T is an interface that implements another interface

I am stuck with a BindingList, where T is the interface that extends the interface A. When I use this bindList in the bindings, only the properties from T are visible, and the properties of the inherited interface A are not. Why is this happening? This looks like a .net error. I demanded that my two projects share some common features. In addition, the binding list has the PropertyDescriptor empty when the PropertyChanged event is tunneled from baseImplementation. Attached Interfaces and Implementations. SetUp method at the end

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; } 
+4
source share
1 answer

Properties are obtained using (indirectly) TypeDescriptor.GetProperties (typeof (T)), but the behavior is as expected. Properties from interfaces are never returned even from class-based models, unless they are in a public API of this type (which for interfaces means an immediate type). Class inheritance is different from the fact that these members are still in the open API. When the interface: ISomeOtherInterface, that is, "implements" rather than "inherits". To give a simple example of when this can be a problem, consider (completely legal):

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

Now; What is IC.Foo?

You may be able to hack this by registering a custom TypeDescriptionProvider for an interface or using ITypedList, but both of them are complex. Honestly, data binding is simply easier to work with classes than with interfaces.

+6
source

Source: https://habr.com/ru/post/1384559/


All Articles