Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity-InjectionProperty会导致空属性吗?_C#_Dependency Injection_Unity Container - Fatal编程技术网

C# Unity-InjectionProperty会导致空属性吗?

C# Unity-InjectionProperty会导致空属性吗?,c#,dependency-injection,unity-container,C#,Dependency Injection,Unity Container,当我 通过容器解析 但是,我现在有一个int属性,我也希望通过容器解析它。它不是在构造函数中传递的,而是作为公共属性传递的。所以我试了一下: [Dependency] container.RegisterType( 新的InjectionProperty(“PropertyName”,1) ); 现在该属性被注入,但所有其他用[Dependency]注释的属性都为null,并且没有解析。如果我对一个属性使用InjectionProperty,我现在是否必须显式声明具有[Dependency]

当我

通过容器解析

但是,我现在有一个int属性,我也希望通过容器解析它。它不是在构造函数中传递的,而是作为公共属性传递的。所以我试了一下:

[Dependency]
container.RegisterType(
新的InjectionProperty(“PropertyName”,1)
);
现在该属性被注入,但所有其他用[Dependency]注释的属性都为null,并且没有解析。如果我对一个属性使用InjectionProperty,我现在是否必须显式声明具有[Dependency]属性的所有其他属性??还是有更好的方法


谢谢。

向API(运行时)注册属性将取消[Dependency]属性。
你不能两者都用。但是您可以使用反射来获取用[Dependency]属性修饰的属性,并在运行时注册它们。

尽管@najmeddine是正确的,但您仍然可以执行以下操作

您的组件:

container.RegisterType<IInterface, MyClass>(
  new InjectionProperty("PropertyName", 1) 
);
注册:

public class Service : IService
{
    [Dependency("Key")]
    public Int32 Value { get; set; }
}
并在
子容器上解析组件

有关容器层次结构的详细信息:

正如najmeddine所说,
InjectionProperty
注册覆盖了
[Dependency]
属性。。。但是,您仍然可以使用
InjectionMethod
对象来不重写
[Dependency]
属性

我使用可选的“覆盖”方法构建对象:

正常DI的工作原理如下:

class SomeObject
{
    [Dependency("Value")]
    public string Value { get; set; }

    public void OverrideValue(string value)
    {
        this.Value = value;
    }
}
container.RegisterInstance(“值”、“默认值”);
container.Resolve();
使用重写的值,您将执行以下操作:

container.RegisterInstance<string>("Value", "Default Value");

container.Resolve<SomeObject>();
container.RegisterType(“NotDefault”,新注入方法(“OverrideValue”,“Other Value”);
container.Resolve(“NotDefault”);
我只是在脑海里写下了这个,所以我为任何打字错误提前道歉。(如果您发现任何问题,请发表评论,我很乐意调整我的答案。)

IUnityContainer child = unity.CreateChildContainer()
    .RegisterInstance("Key", 1900);
class SomeObject
{
    [Dependency("Value")]
    public string Value { get; set; }

    public void OverrideValue(string value)
    {
        this.Value = value;
    }
}
container.RegisterInstance<string>("Value", "Default Value");

container.Resolve<SomeObject>();
container.RegisterType<SomeObject>("NotDefault", new InjectionMethod("OverrideValue", "Other Value"));

container.Resolve<SomeObject>("NotDefault");