简体   繁体   English

从非泛型基类继承的泛型子类的实例化对象

[英]Instantiating object of generic subclass that inherits from non-generic base class

I have a base class like so: 我有一个像这样的基类:

public class Base{
    private string name = string.Empty();

    public Base(string name){
        this.name = name;
    }

    public string Name{get;set;}
}

and I have a generic class that inherits from the Base class and also contains a method that returns an object of the generic type T like so : 我有一个继承自Base类的泛型类,并且还包含一个返回泛型T的对象的方法,如下所示:

public SubClass<T>:Base{
    public SubClass(string name):base(name){}

    public T method(string parameter){
        //do some stuff here and return T
        return T;
    }
}

and I instantiate an object of the SubClass : 然后实例化SubClass的对象:

object instance = new SubClass<object>("name");

and I don't use the generic type in the constructor. 而且我不在构造函数中使用泛型类型。 the parameters in the SubClass constructor are predefined types (eg string, int etc.). SubClass构造函数中的参数是预定义的类型(例如,字符串,整数等)。 This implementation works fine BUT i'm wondering if this is this correct? 这种实现效果很好,但是我想知道这是否正确吗? is there another more appropriate way to do this. 还有另一种更合适的方法可以做到这一点。 Thanx 谢谢

Edit 编辑

The context: The Base class is a class that handles the connection with CouchDB. 上下文: Base类是处理与CouchDB的连接的类。 so i provide the necessary info(username, pass, host, port and database name) and the SubClass is a simple client for CouchDB. 所以我提供了必要的信息(用户名,密码,主机,端口和数据库名称),并且SubClass是CouchDB的简单客户端。 So when I create an object of the SubClass I want to provide the credentials for the CouchDB and I want, also, to provide the model (eg Account model, Product model) that I expect from the database. 因此,当我创建SubClass的对象时,我想提供CouchDB的凭据,我也想提供我期望从数据库中获得的模型(例如,帐户模型,产品模型)。

I am a bit puzzled looking at your code. 我对您的代码有些困惑。 Why do you store your instance of SubClass in an object? 为什么将SubClass实例存储在对象中? That way you won't be able to invoke any members of your class (others than those inherited from System.Object ). 这样,您将无法调用类的任何成员(从System.Object继承的成员除外)。

I want, also, to provide the model (eg Account model, Product model) that I expect from the database 我也想提供我希望从数据库中获得的模型(例如,帐户模型,产品模型)

I'll assume these different types of models implement a common interface, so the structure looks something like this: 我假设这些不同类型的模型实现一个公共接口,所以结构看起来像这样:

public interface IModel
{
    Foo GetFoo();
}

public class AccountModel : IModel
{
    public Foo GetFoo()
    {
        // whatever
    }
}

public class ProductModel : IModel
{
    public Foo GetFoo()
    {
        // whatever
    }
}

Since we know that the type parameter of SubClass is an IModel , it would be a good idea to place a type constraint on T : 因为我们知道SubClass的类型参数是IModel ,所以最好在T上放置类型约束:

public class SubClass<T> : Base where T : IModel
{
    public SubClass(string name) : base(name){}

    public T Method(string parameter)
    {
        //do some stuff here and return T
        return T;
    }
}

And I'd use it like this: 我会这样使用它:

SubClass<ProductModel> pm = new SubClass<ProductModel>("blurg");
IModel model = pm.Method();
Foo foo = model.GetFoo();

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

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