Collection
プロパティを持つタイプ(以下の例ではMyType
)を実装しています。内にはMyType
私は実際にどのようなコレクションの種類が気にしません。私が気にしているのは、それがIEnumerable<String>
とINotifyCollectionChanged
です。これらの制限を使用してCollection
プロパティを実装するにはどうすればよいですか?ここでは、インターフェイスの継承が適切なのでしょうか。なぜ機能しませんか?
を私は新しいインターフェース作成:
interface INotifyEnumerableCollectionChanged<T> : IEnumerable<T>, INotifyCollectionChanged {}
をし、タイプINotifyEnumerableCollectionChanged<String>
のMyType
にCollection
財産を作ったここ
は、私が試したものです。これはMyType
の内部で動作するようです。私はそのコレクションを列挙してCollectionChanged
イベントを登録できるようです。
しかし、私はさえ厳しいMyCollection
はIEnumerable<String>
とINotifyCollectionChanged
の両方を実装し、コレクション(以下の例ではMyCollection
)にこのプロパティを設定することはできません。
コンパイラは言う:
が暗黙的にタイプ 'InterfaceInheranceTest.Program.MyCollection' は に変換できません 'InterfaceInheranceTest.Program.INotifyEnumerableCollectionChanged'。 明示的な変換が存在する(あなたが キャストが欠けている?)
これを行うには正しい方法は何ですか?ここで
は、完全なサンプルコードです:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
namespace InterfaceInheranceTest
{
class Program
{
interface INotifyEnumerableCollectionChanged<T> : IEnumerable<T>, INotifyCollectionChanged {}
class MyCollection : IEnumerable<String>, INotifyCollectionChanged
{
IEnumerator<String> IEnumerable<String>.GetEnumerator()
{ throw new NotImplementedException(); }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{ throw new NotImplementedException(); }
public event NotifyCollectionChangedEventHandler CollectionChanged;
}
class MyType
{
private INotifyEnumerableCollectionChanged<String> _Collection;
public INotifyEnumerableCollectionChanged<String> Collection
{
get { return _Collection; }
set
{
_Collection = value;
_Collection.CollectionChanged += new NotifyCollectionChangedEventHandler(_Collection_CollectionChanged);
foreach (var item in _Collection)
{
Console.WriteLine(item);
}
}
}
void _Collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{ throw new NotImplementedException(); }
}
static void Main(string[] args)
{
var collection = new MyCollection();
var type = new MyType();
type.Collection = collection; // compiler doesn't like this!
}
}
}
コンパイル済みです。しかし、なぜこれが必要ですか?すでに 'IEnumerable'と 'INotifyCollectionChanged'を実装しています。そして、 'INotifyEnumerableCollectionChanged 'は実際に何も追加しません。 –
@RobertHegner - 使用する変数のタイプを 'INotifyEnumerableCollectionChanged'と定義しています。実装はこのインタフェースを実装していないため、このインタフェースを有効な参照として使用することはできません。 –
Oded
あなたのソリューションと説明に感謝します!私は4分で受け入れるだろう... :) –