[英]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.