[英]How to create a refence a generic class in a non generic class
有沒有一種方法可以在非通用類中引用通用類,例如
public class IGenericClass<T> where T: class
{
}
public class GenericClass<T> : IGenericClass<T> where T : class
{ }
我如何最好地在具體的類中為IGenericClass創建引用。
謝謝你的協助
是的你可以。 但是,它應該是封閉構造類型,這意味着T
應該預先知道。
class Client
{
private IGenericClass<T> instance;// Illegal[1]
private IGenericClass<object> instance;// Legal, It can be any type.
}
另外,不要使用I Prefix命名類,這可能會給人以為它不是接口的印象。
1.如果您有一個名為T
的類型是合法的,否則
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.