【发布时间】:2012-08-26 03:19:22
【问题描述】:
我有一个名为 gameObject 的类,其中一个属性名为 component,类型为 object:
public object component;
我正在尝试使用这个object 作为一个对象,它可以保存你给它的任何类的对象。例如
unit c = new unit(...)
gameObject test = new gameObject(...)
test.component = c;
我想通过组件对象使用 c 对象。例如
if(test.component==typeof(unit))
test.component.Draw();//draw is a function of the unit object
这可能吗?我该怎么做?
【问题讨论】:
-
您可以指定为组件的类型范围有哪些?你能想到他们应该有的一组共同行为吗?
-
如果您使用泛型,您可以将 component 设为强类型属性。
标签: c# variables object typeof