简体   繁体   English

将接口传递给 C# 中类的通用属性?

[英]Pass Interface to a generic attribute of a class in C#?

When a do this:当这样做时:

public class ball {
    ITeste<IItem> Teste{ get; set; }
}

I got the error:我得到了错误:

'IItem' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method “IItem”必须是具有公共无参数构造函数的非抽象类型,以便将其用作泛型类型或方法中的参数“T”

ITeste:测试:

public interface ITeste<T> 
        where T : IItem, IEquatable<T>, new()
{
    //ITeste code
}

IItem:项目:

public interface IItem : IEquatable<IItem>
{
    //IItem Code
}

It seems to be a simple question, but I could not find the answer.这似乎是一个简单的问题,但我找不到答案。 Any help it would be appreciated.任何帮助将不胜感激。

Remove new() from the constraints for ITeste从 ITste 的约束中删除 new()

ITeste:测试:

public interface ITeste<T>where T : IItem, IEquatable<T>
{
    //ITeste code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM