[英]Using generic interface in a generic class
如何強制使用在類中實現泛型接口的類? 這樣的事情可能嗎?
public interface IGeneric<T>
{
T Value {get;}
}
//public class MyList<IGeneric<T>> {}//not allowed
像這樣的東西:
void Main()
{
MyList<string> myList = new MyList<string>(new Generic());
}
public interface IGeneric<T>
{
T Value { get; }
}
public class MyList<T>
{
private IGeneric<T> _generic;
public MyList(IGeneric<T> generic)
{
_generic = generic;
}
}
public class Generic : IGeneric<string>
{
public string Value => throw new NotImplementedException();
}
或者像這樣:
void Main()
{
MyList<Generic, string> myList = new MyList<Generic, string>();
//Or MyList<IGeneric<string>, string> myList = new MyList<IGeneric<string>, string>();
}
public interface IGeneric<T>
{
T Value { get; }
}
public class MyList<G, T> where G : IGeneric<T>
{
}
public class Generic : IGeneric<string>
{
public string Value => throw new NotImplementedException();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.