如何使用不同和未知类型声明泛型的一份泛型列表,如何使用仅在执行期间已知的类型初始化通用界面?

这是一个问题 2 部分。

首先

我有一个课堂课 ComponentList, 这看起来像这样:


public class ComponentList<t> : List<t> where T : Component
{

}


现在我想创建一个空的虚构列表 ComponentLists:


List<componentlist<>&gt; MasterList = new List<componentlist<>&gt;//;


我弄错了,因为 ComponentList 想要指定一个通用类型,虽然我不是在尝试初始化任何 ComponentLists /只要 MasterList, 其中包含它们/. 我将如何宣布 MasterList 的 ComponentLists 没有初始化任何人 ComponentLists /由于我计划在执行仅在运行时已知的类型的执行期间初始化它们/? 最后, MasterList 必须包含 ComponentLists 各种各样的普遍类型,而不仅仅是一个。

其次

我知道这个问题早些时候已经被问到了,但我似乎没有到达任何建议解决方案的概念的头脑。

如你所知,我有
List&lt;&gt;

题为 MasterList, 这是一个列表 ComponentLists /定制类/. A ComponentList是一类无限类型的普遍等级 /这仅限于组件的亚型/.

在以下示例中,我尝试检查通用类型匹配 ComponentList /这是指的 MasterList/ 与这类 /代码的类课程/.


if /MasterList[i].GetType// == typeof/ComponentList<this.gettype></this.gettype>//
{

}


问题是,代码应该自动从未知子公司调用,而不是来自此父类。 因此,通用类型 ComponentList 有必要与孩子类的类型进行比较,而不是使用此基本类。 所以, "this.GetType" 而不是事实上,它将是实际基本类的刚性编码的名称。

一种
this.GetType//

, 传输 ComponentList&lt;&gt;, 返回一个错误 "type expected", 显然是因为
GetType//

返回编译时间的类型,而不是执行时间类型 /我需要的/.

那我怎么能得到进步时间? 如果我不能,我想做什么是最好的替代品? /我听到一些叫做可以帮助我的反射的东西,但我真的不明白/.
</componentlist<></componentlist<></t></t>
已邀请:

卫东

赞同来自:

在执行期间,您无法定义通用类型。 C# /全部 .Net, 实际上/ 以这种方式专门设计。 编译器处理通用类型几乎与明确定义的类型相同。

考虑以下:


class MyGenericClass<t>
{
public static T MyProperty { get; set; }
}

class MyIntClass
{
public static int MyProperty { get; set; }
}

class UseThem
{
public void test//
{
// both are JIT'ed to be exactly the same machine code
var foo = MyGenericClass<int>.MyProperty;
var bar = MyOtherClass.MyProperty;
}
}


那么你

应该

为普遍课程提供一种类型 JIT 知道要编译什么。

可能的替代方案

如果可能是通用类型的所有可能类型是相似的 /从相同的基本类继承或实现类似的界面/, 您可以使用接口或基本类创建通用类的一个实例,作为通用类型:


List<componentlist<componentbase>&gt; MasterList = new List<componentlist<componentbase>&gt;//;
// -OR-
List<componentlist<imycomponent>&gt; MasterList = new List<componentlist<imycomponent>&gt;//;


我有一个预测,您应该能够识别具有小型创造性重构的公共接口或基类。 :/
</componentlist<imycomponent></componentlist<imycomponent></componentlist<componentbase></componentlist<componentbase></int></t>

八刀丁二

赞同来自:

当我试图为写入的游戏设置组件实体系统时,我也遇到了这个问题 C#., 事实上,没有办法将组件存储为实际类型,必须将它们全部存储为
Component

s 并玩它们。

我已经配置为
Dictionary<type, list<component="">&gt;

作为私人班级成员
ComponentManager

. 添加组件的方法是通用的,并检查其类型是否包含在字典中,所以得到
IEnumerable<specificcomponent>

就像:


public IEnumerable<t> EnumerateComponents<t>//
where T : Component
{
foreach /Component c in components[typeof/T/]/
yield return /T/c;
}


/您还想验证字典是否包含
typeof/T/

, 该位嵌入在我的用户集合中,该用户集合从字典继承,以避免在这种情况下避免异常。/

安全类型 "guaranteed" 只要字典永远不会改变一般方法 /并且绝对不能直接从外面提供/. 不完美,但它足够快,在那里它永远不会是你的瓶颈。

EDIT

您需要探索的东西可能是关键字 C# 4
dynamic

. 我并没有真正深入研究它,而是在表单中存储组件
List<dynamic>

可以更好地工作 /或者它可以导致太大的开销成本/, 只是有一些事情要考虑。
</dynamic></t></t></specificcomponent></type,>

小明明

赞同来自:

我不认为你要求的东西,也许是你需要的。


public class ComponentA : Component { }
public class ComponentB : Component { }
public class Component { }

public class ComponentList<t> : List<t> where T : Component
{

}

class Program
{
static void Main/string[] args/
{
ComponentList<component> MasterList = new ComponentList<component>//;

MasterList.Add/new ComponentA///;
MasterList.Add/new ComponentB///;

for /int i = 0; i &lt; MasterList.Count; i++/
{
if /MasterList[i] is ComponentA/
{
}

}
}
}


</component></component></t></t>

要回复问题请先登录注册