Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ICollection其中T实现一个接口_C#_.net - Fatal编程技术网

C# ICollection其中T实现一个接口

C# ICollection其中T实现一个接口,c#,.net,C#,.net,有没有办法做这样的事 public interface ISomething { } public class SomethingThatImplementsISomething: ISomething { } public class SomethingElse { public ICollection<ISomething> PropertyName{ get; set; } } 我试过了,但一直失败。有什么想法吗?确保在编译单元的顶部使用System.Collectio

有没有办法做这样的事

public interface ISomething
{
}

public class SomethingThatImplementsISomething: ISomething
{
}

public class SomethingElse
{
 public ICollection<ISomething> PropertyName{ get; set; }
}
我试过了,但一直失败。有什么想法吗?

确保在编译单元的顶部使用System.Collections.Generic,而不仅仅是System.Collections

编辑以添加:

实际上,这可能与4.0之前的C版本不允许泛型的协方差或逆变这一事实有关。在旧版本中,必须定义泛型字段以使用精确的数据类型,以避免无效的引用类型分配

再次编辑:

现在我看到了您收到的实际错误消息,问题不在于字段的定义,而在于它的使用。您需要在分配中放入显式强制转换。

自动属性

 public ICollection<ISomething> PropertyName{ get; set; }
尝试创建ICollection类型的支持字段,但将失败。尝试以下方法:

 public List<ISomething> PropertyName{ get; set; }
ICollection无法转换为ICollection

如果有人试图向收藏中添加一个不是模型的iWardable,会发生什么


只需实例化一个iWardable集合,并添加模型的实例。对其进行奖励。

听起来您要做的是:

ICollection<SomethingThatImplementsISomething> collection = new List<SomethingThatImplementsISomething>();
somethingElse.PropertyName = collection;
然后,您可以向您的心形内容中添加一些东西,如工具或对象:

somethingElse.PropertyName.Add(new SomethingThatImplementsISomething());

你说不断失败是什么意思?它……不起作用。它说我不能将实现接口的类传递到集合的新实例中。但是将属性设置为ICollection可以让我实例化任何实现ICollection的东西,比如List等。我不明白为什么它不能用于集合中的泛型类型。你说集合中的泛型类型是什么意思?ISomething不是泛型。-1-为什么每个人都要猜测问题是什么?发布一个包含足够信息的完整问题,以便人们能够回答。为什么会失败?有一个ICollection类型的支持字段/autoprop是完全可以的。你只需要记住在使用它之前将它设置为一个具体的类型,比如List。是的,我在实例化时将它设置为List,并传递一个实现接口的类型。还是不行。我在文件中声明了正确的名称空间。不,它初始化为null。初始化可以在构造函数中被覆盖。我已经尝试过几次了,但我猜在泛型中语法很奇怪。我怎样才能在泛型声明中进行显式转换呢?天哪,我觉得自己太傻了。这样的入门级C,我被错误缠住了,我忽略了认识到接口抽象正是我希望它工作的原因。非常感谢你!注意,在C4中,您可以将IEnumerable转换为IEnumerable,但不能将ICollection转换为ICollection。区别在于:既然不能向IEnumerable中添加东西,那么添加的反对意见就消失了。是的,这是正确的。我忘了泛型类型只是一个类型,而不是一个对象。我试图使用实现接口的类实例化泛型,而不是使用接口实例化并分配实现接口的类的对象。我现在知道错误在哪里了。
somethingElse.PropertyName.Add(new SomethingThatImplementsISomething());