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
是限制性的。这是解决我问题的正确方法。非常感谢。结果证明这是解决我问题的正确方法。非常感谢。