4

T が A インターフェイスを拡張するインターフェイスである BindingList にこだわっています。この bindingList をバインディングで使用すると、T からのプロパティのみが表示され、継承された A インターフェイスからのプロパティは表示されません。なぜそれが起こっているのですか?.net のバグのようです。これは、2 つのプロジェクトで共通の機能を共有するために必要でした。また、PropertyChanged イベントが baseImplementation からトンネリングされると、バインディング リストの 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;
    }
4

1 に答える 1

6

プロパティは (間接的に) TypeDescriptor.GetProperties(typeof(T)) を介して取得されますが、動作は期待どおりです。インターフェイスからのプロパティは、クラスベースのモデルからであっても、その型のパブリック API にない限り、返されることはありません (インターフェイスの場合は、即時型を意味します)。これらのメンバーはまだパブリック API にあるため、クラスの継承は異なります。インターフェイスの場合: ISomeOtherInterface、つまり「継承」ではなく「実装」です。これが問題になる可能性がある場合の簡単な例を示すために、次のことを考慮してください (完全に合法です):

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

今; IC.Foo とは何ですか?

インターフェイスのカスタム TypeDescriptionProvider を登録するか、ITypedList を使用することで、これを回避できる可能性がありますが、どちらも注意が必要です。正直なところ、データ バインディングはインターフェイスよりもクラスの方が簡単に機能します。

于 2011-12-04T11:24:19.190 に答える