簡體   English   中英

如何在非通用類中創建引用通用類

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM