C# 是否可以将具有不同泛型/类型作为参数的对象列表

C# 是否可以将具有不同泛型/类型作为参数的对象列表,c#,generics,C#,Generics,考虑以下代码: class Collision<type1, type2> { // further Code } 通过这种方法,我使用基类单位来替代两个子类玩家和敌人。但是我还有一个类环境,它不是子类,但也应该是冲突的可能参数 提前谢谢 集合只能对T、K或其子对象执行操作。如果单元和环境没有直接关系,您可以使用对象: Collision<object, object> 冲突 对于“或其子女”,您必须使用“”: 接口碰撞{} 类冲突:ICollision{

考虑以下代码:

class Collision<type1, type2> 
{
    // further Code
}
通过这种方法,我使用基类
单位
来替代两个子类
玩家
敌人
。但是我还有一个类
环境
,它不是子类,但也应该是
冲突
的可能参数

提前谢谢

集合只能对
T
K
或其子对象执行操作。如果
单元
环境
没有直接关系,您可以使用
对象

Collision<object, object>
冲突
对于“或其子女”,您必须使用“”:

接口碰撞{}
类冲突:ICollision{}
然后列一张清单如下:

var collisionList = new LinkedList<ICollision<Unit, Unit>>();
var collisionList=newlinkedlist();

您需要接口,因为类不支持协方差,但是您可以将例如
冲突
添加到列表中。

一种方法是使用接口:

interface ICollision
{
    object PropertyOfType1{get;}
    object PropertyOfType2{get;}
}
class Collision<type1,type2>:ICollision
{
    public type1 PropertyOfType1 {get;set;}
    public type2 PropertyOfType2 {get;set;}

    object ICollision.PropertyOfType1
    {
        get{return PropertyOfType1;}
    }

    object ICollision.PropertyOfType2
    {
        get{return PropertyOfType2;}
    }
}
LinkedList<ICollision> collisionList  = new LinkedList<ICollision>();

collisionList.AddLast(new Collision<int,int>());
collisionList.AddLast(new Collision<double,double>());
然后是从该接口继承的泛型类:

interface ICollision
{
    object PropertyOfType1{get;}
    object PropertyOfType2{get;}
}
class Collision<type1,type2>:ICollision
{
    public type1 PropertyOfType1 {get;set;}
    public type2 PropertyOfType2 {get;set;}

    object ICollision.PropertyOfType1
    {
        get{return PropertyOfType1;}
    }

    object ICollision.PropertyOfType2
    {
        get{return PropertyOfType2;}
    }
}
LinkedList<ICollision> collisionList  = new LinkedList<ICollision>();

collisionList.AddLast(new Collision<int,int>());
collisionList.AddLast(new Collision<double,double>());
类冲突:ICollision
{
公共类型1属性类型1{get;set;}
公共类型2属性类型2{get;set;}
对象ICollision.PropertyOfType1
{
获取{return PropertyOfType1;}
}
对象ICollision.PropertyOfType2
{
获取{return PropertyOfType2;}
}
}
然后,您可以使用以下界面创建链接列表:

interface ICollision
{
    object PropertyOfType1{get;}
    object PropertyOfType2{get;}
}
class Collision<type1,type2>:ICollision
{
    public type1 PropertyOfType1 {get;set;}
    public type2 PropertyOfType2 {get;set;}

    object ICollision.PropertyOfType1
    {
        get{return PropertyOfType1;}
    }

    object ICollision.PropertyOfType2
    {
        get{return PropertyOfType2;}
    }
}
LinkedList<ICollision> collisionList  = new LinkedList<ICollision>();

collisionList.AddLast(new Collision<int,int>());
collisionList.AddLast(new Collision<double,double>());
LinkedList冲突列表=新建LinkedList();
AddLast(new Collision());
AddLast(new Collision());

我认为您需要类似于
类MyClass:LinkedList的东西
只能对T、K或其子对象进行操作?你确定吗?-1类不支持协方差!,如果您编辑了误导性的部分,则Downvote将被删除。如果我理解正确,那么除了先将参数类型强制转换为上层类,然后在以后使用时将其强制转换回实际类型之外,没有其他选项了?@user3293330:是,但“上层类”不需要强制转换。但是请看Albertos的答案,因为这是一种比使用
object
更好的方法。与我即将发布的答案类似,但并不完全相同。回答得好。这是一条路,比我的
对象
建议更好。