我在我的游戏中使用自定义属性来允许我定义聚合组件之间的依赖关系。在自定义属性中使用'新SomeObject'
[ComponentDependency(typeof(SomeDependentComponent))]
class SomeComponent : Component {}
但是,这意味着我必须使用默认值,我想补充这样每个组件。我希望能够做到:
[ComponentDependency(typeof(SomeDependentComponent), ctrParam1, ctrParam2...)]
,并直接喂到这些Activator.CreateInstance(Type, object[])
,但我得到的错误。我认为这是关于编译时的属性。我对他们了解不多。
这可能吗?
编辑:如果我使用的参数,它可能看起来像: [ComponentDependency(typeof(PositionalComponent), new Vector2(300, 300))]
参数是什么? – SLaks 2011-06-14 15:22:33
这听起来像你会更好地使用像NInject或Autofac这样的依赖注入容器,而不是试图编写自己的。 – mdm 2011-06-14 15:24:34
@mdm目前查找Ninject,它似乎是有道理的.sort of。 – 2011-06-14 15:43:22