0

我需要根据PropertyInfo.Name来解析属性注入。这个类看起来像这样属性解析策略

public class Test 
{ 
    [Dependency] 
    public Test TestProperty1{ get; set; } 
} 

我想以某种方式能够解决TestProperty1这样幕后背后,没有明确地重新键入属性名称。

container.Resolve<Test>("TestProperty1") 

期间要么container.Resolve<Test>()container.BuildUp(new Test())通话**

我怎样才能做到这一点?

+0

您的设计有问题。解决'Test'可能会导致堆栈溢出异常,因为它依赖于它自己。你想解决什么问题? – Steven

回答

0

我不知道我是否理解你想达到的目标。你希望Unity在不告诉它的情况下填充TestProperty1

使用DependencyAttribute不是一个好选择。 This article explains why

要指示统一注入一个值TestProperty1必须修改注册码:

container.RegisterType<Test>(new InjectionProperty("TestProperty1")); 

呼叫解决将返回的Test一个实例,其TestProperty1填补。

如果您有一个Test的现有实例,则调用container.BuildUp()也会为TestProperty1注入一些值。