Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在csharp中使用泛型类型继承_C#_Templates_Generics_Inheritance - Fatal编程技术网

C# 如何在csharp中使用泛型类型继承

C# 如何在csharp中使用泛型类型继承,c#,templates,generics,inheritance,C#,Templates,Generics,Inheritance,我有以下示例摘要通用代码: public class Box<E> where E : BoxProperties{ } public class BoxProperties{ } public class BlueBox : Box<BlueBox.BlueProperties >{ public class BlueProperties : Properties{ } } public class RedBox : Box<RedBox.R

我有以下示例摘要通用代码:

public class Box<E> where E : BoxProperties{
}

public class BoxProperties{
}

public class BlueBox : Box<BlueBox.BlueProperties >{
    public class BlueProperties : Properties{
    }
}

public class RedBox : Box<RedBox.RedProperties >{
    public class RedProperties : Properties{
    }
}
公共类框,其中E:BoxProperties{
}
公共类属性{
}
公共类蓝盒子:盒子{
公共类BlueProperties:属性{
}
}
公共类RedBox:Box{
公共类RedProperties:属性{
}
}
我需要创建一个字典,可以存储RedBox和BlueBox作为值。
有什么帮助吗?

对于您描述的类型,在
BlueBox
RedBox
之间最近的共同祖先类型是
System.Object
。您必须使用
字典
,或者引入一些其他公共祖先类型。

对于您描述的类型,在
蓝盒
红盒
之间最近的公共祖先类型是
系统.对象
。您必须使用
字典
,或者引入一些其他常见的祖先类型。

我认为这取决于类

接口IBox,其中E:BoxProperties
{
}
公共类Box:IBox,其中E:BoxProperties
{
}
公共类属性
{
}
公共类蓝盒子:盒子
{
公共类BlueProperties:BoxProperties
{
}
}
公共类RedBox:Box
{
公共类RedProperties:BoxProperties
{
}
}
使用此功能,您可以声明字典,如:

var dic = new Dictionary<string, IBox<BoxProperties>>();

dic.Add("red", new RedBox());
dic.Add("blue", new BlueBox());
var dic=newdictionary();
添加(“红色”,新的红色框());
dic.添加(“蓝色”,新的蓝盒());

但是
out
对你来说不是最好的,看看我想这取决于什么是class

接口IBox,其中E:BoxProperties
{
}
公共类Box:IBox,其中E:BoxProperties
{
}
公共类属性
{
}
公共类蓝盒子:盒子
{
公共类BlueProperties:BoxProperties
{
}
}
公共类RedBox:Box
{
公共类RedProperties:BoxProperties
{
}
}
使用此功能,您可以声明字典,如:

var dic = new Dictionary<string, IBox<BoxProperties>>();

dic.Add("red", new RedBox());
dic.Add("blue", new BlueBox());
var dic=newdictionary();
添加(“红色”,新的红色框());
dic.添加(“蓝色”,新的蓝盒());

但是,
out
不可能是最适合你的,看看

你可以这样做-

public class BoxProperties
{
}
interface IBox
{

}

public class Box<E> : IBox where E : BoxProperties
{
}

public class BlueBox : Box<BlueBox.BlueProperties>
{
    public class BlueProperties : BoxProperties
    {
    }
}

public class Properties
{
}

public class RedBox : Box<RedBox.RedProperties>
{
    public class RedProperties : BoxProperties
    {
    }
}
公共类属性
{
}
接口IBox
{
}
公共类Box:IBox,其中E:BoxProperties
{
}
公共类蓝盒子:盒子
{
公共类BlueProperties:BoxProperties
{
}
}
公共类属性
{
}
公共类RedBox:Box
{
公共类RedProperties:BoxProperties
{
}
}
有了这一点,您将能够做到-

        var dictionary = new Dictionary<string, IBox>();
        dictionary.Add("a", new BlueBox());
        dictionary.Add("b", new RedBox());
var dictionary=newdictionary();
添加(“a”,新的BlueBox());
添加(“b”,新的RedBox());

您可以这样做-

public class BoxProperties
{
}
interface IBox
{

}

public class Box<E> : IBox where E : BoxProperties
{
}

public class BlueBox : Box<BlueBox.BlueProperties>
{
    public class BlueProperties : BoxProperties
    {
    }
}

public class Properties
{
}

public class RedBox : Box<RedBox.RedProperties>
{
    public class RedProperties : BoxProperties
    {
    }
}
公共类属性
{
}
接口IBox
{
}
公共类Box:IBox,其中E:BoxProperties
{
}
公共类蓝盒子:盒子
{
公共类BlueProperties:BoxProperties
{
}
}
公共类属性
{
}
公共类RedBox:Box
{
公共类RedProperties:BoxProperties
{
}
}
有了这一点,您将能够做到-

        var dictionary = new Dictionary<string, IBox>();
        dictionary.Add("a", new BlueBox());
        dictionary.Add("b", new RedBox());
var dictionary=newdictionary();
添加(“a”,新的BlueBox());
添加(“b”,新的RedBox());

BlueProperties
RedProperties
继承自属性,因此只需创建一个
字典
@alesandrod'Andria,它不会帮助他存储
RedBox
BlueBox
对象。无论是
RedBox
还是
BlueBox
继承自
Properties
@cdhowie你是对的,我不理解这个问题。
BlueProperties
RedProperties
继承自Properties,所以只需创建一个
字典
@AlessandroD'Andria,它不会帮助他存储
RedBox
BlueBox
对象。无论是
RedBox
还是
BlueBox
都不是从
Properties
@cdhowie继承的。你是对的,我不理解这个问题。我希望这是选择的答案,是目前为止最好的方法。这取决于课程的设计,输出的
是有限制的。我希望这是选择的答案,是目前为止最好的方法。这取决于类的设计,
out
是限制性的。这是解决我问题的正确方法。非常感谢。结果证明这是解决我问题的正确方法。非常感谢。