简体   繁体   English

如何在非通用类中创建引用通用类

[英]How to create a refence a generic class in a non generic class

is there a way of referencing a generic class in a non generic class eg 有没有一种方法可以在非通用类中引用通用类,例如

public class IGenericClass<T> where T: class
{
}

public class GenericClass<T> : IGenericClass<T> where T : class
{   }

how best do i create a refence for the IGenericClass in a concrete class. 我如何最好地在具体的类中为IGenericClass创建引用。

thanks for your assistance 谢谢你的协助

Yes, you can. 是的你可以。 However, it should be a closed constructed type, meaning T should be known upfront. 但是,它应该是封闭构造类型,这意味着T应该预先知道。

class Client
{
    private IGenericClass<T> instance;// Illegal[1]
    private IGenericClass<object> instance;// Legal, It can be any type.
}

Also, don't name classes with I Prefix which may give the impression that it is an interface when it is not. 另外,不要使用I Prefix命名类,这可能会给人以为它不是接口的印象。

1. It is legal if you have a type named T , otherwise not 1.如果您有一个名为T的类型是合法的,否则

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

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