Skip to main content
 首页 » 编程设计

c#之接口(interface)继承在这里是正确的做法吗为什么它不起作用

2024年02月23日15落叶无声

我正在实现某种类型(在下面的示例中为 MyType),它有一个 Collection。属性(property)。里面MyType我真的不在乎这是一个什么样的集合。我唯一关心的是它实现了 IEnumerable<String>和 INotifyCollectionChanged .我将如何实现 Collection有这些限制的属性(property)?

这是我尝试过的:

我创建了一个新界面:

interface INotifyEnumerableCollectionChanged<T> : IEnumerable<T>, INotifyCollectionChanged {} 

并制作了Collection属性(property) MyType类型 INotifyEnumerableCollectionChanged<String> .这似乎在 MyType 内部起作用.看起来我可以枚举那个集合并注册 CollectionChanged事件。

但我无法将此属性设置为集合(下例中的 MyCollection),即使是 MyCollection 也是如此。同时实现 IEnumerable<String>和 INotifyCollectionChanged .

编译器说:

Cannot implicitly convert type 'InterfaceInheranceTest.Program.MyCollection' to 'InterfaceInheranceTest.Program.INotifyEnumerableCollectionChanged'. An explicit conversion exists (are you missing a cast?)

正确的做法是什么?

这里是完整的示例代码:

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! 
        } 
    } 
} 

请您参考如下方法:

你的类需要实现 INotifyEnumerableCollectionChanged<T> .

    class MyCollection : INotifyEnumerableCollectionChanged<string>.  
                         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; 
    } 

这是必需的,因为您使用的变量 ( _collection ) 的类型是 INotifyEnumerableCollectionChanged<T> .然而,你的类(class) - MyCollectionChanged没有实现这个接口(interface),所以不能作为对变量的引用赋值。