扩展使用C#泛型? [英] Extending using C# generics?

查看:106
本文介绍了扩展使用C#泛型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个可扩展的嵌套结构,看起来我应该可以使用泛型来做到这一点,尽管我可能没有正确使用它们。



我想能够从GroupType和/或OptionType创建子类。问题是我不能对泛型类型执行 new 操作,即使我指定它们只能是某种基类型。



有没有办法做我想做的事情?

  public class AllInfo< GroupType,OptionType> 
其中GroupType:GroupBase< OptionType>
其中OptionType:OptionBase
{
public List< string>名称{set;得到; }
public List< GroupType>组{set;得到; }
$ b $ public AllInfo()
{
DataSet ds = DatabaseRetreival();
this.Groups.add(new GroupType(ds [Name],ds [Type]));
}

}

公共类GroupBase< OptionType>
where OptionType:OptionBase
{
public string Name {set;得到; }
public string Type {set;得到; }
public List< OptionType>选项{set;得到; }
$ b $公共GroupBase(字符串名称,字符串类型)
{
this.Name = name;
this.Type = type;

DataSet ds = DatabaseRetreival(this.Type);
this.Options.Add(new OptionType(ds [Name]));
}
}

public class OptionBase
{
public string Name {set;得到; }

public OptionBase(string name)
{
this.Name = name;



$ div $解析方案

你无法指定泛型类应该具有哪些构造函数。构造函数不是继承的,所以即使你指定的基类具有该构造函数,从它派生的类也不必具有该构造函数。



您可以要求的构造函数是无参数的构造函数:

 其中GroupType:GroupBase< OptionType> ;, new()

因为只有让我们使用无参数构造函数,您还可以使用虚拟方法将数据放入对象中,例如:

  GroupType group = new GroupType(); 
group.Init(ds [Name],ds [Type]);
this.Groups.add(group);


I want to create an extendable nested structure and it seems like I should be able to do this using generics, though I may not be using them "properly".

I want to be able to create child classes from GroupType and/or OptionType. The problem is that I can't perform the new operation on the generic types even though I specified they could only be of a certain base type.

Is there any way to do what I'm trying to do?

public class AllInfo<GroupType, OptionType> 
    where GroupType: GroupBase<OptionType>
    where OptionType: OptionBase
{
    public List<string> Names { set; get; }
    public List<GroupType> Groups { set; get; }

    public AllInfo()
    {
        DataSet ds = DatabaseRetreival();
        this.Groups.add(new GroupType(ds["Name"], ds["Type"]));
    }

}

public class GroupBase<OptionType> 
    where OptionType: OptionBase
{
    public string Name { set; get; }
    public string Type { set; get; }
    public List<OptionType> Options { set; get; }

    public GroupBase(string name, string type)
    {
         this.Name = name;
         this.Type = type;

         DataSet ds = DatabaseRetreival(this.Type);
         this.Options.Add(new OptionType(ds["Name"]));
    }
}

public class OptionBase
{
    public string Name { set; get; }

    public OptionBase(string name)
    {
        this.Name = name;
    }
}

解决方案

You can't specify which constructors a generic class should have. The constructors are not inherited, so even if the base class that you specified has that constructor, a class that derives from it doesn't have to have that constructor.

The only constructor that you can require is the parameterless constructor:

where GroupType: GroupBase<OptionType>, new()

As that only let's you use the parameterless constructor, you would also use a virtual method for putting the data in the object, for example:

GroupType group = new GroupType();
group.Init(ds["Name"], ds["Type"]);
this.Groups.add(group);

这篇关于扩展使用C#泛型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆