Search code examples
c#dependency-injectionattributesunity-containerdefault-constructor

What name will DependencyAttribute instance have if default constructor is used?


I have a code:

[Dependency] public string MyProperty { get; set; }

While DependencyAttribute class has property Name.

What value will it contain in this case?

Will it be null or nameof(MyProperty)?

Unfortunately I have not seen source code of the DependencyAttribute class yet. Maybe someone can provide the link.

Thanks in advance!


Solution

  • The value will be null. In the absence of an explicit name, null is the default name used by Unity (e.g. in calls toRegisterType()). In terms of DependencyAttribute specifically:

        public DependencyAttribute()
            : this(null) { }
    
        /// <summary>
        /// Create an instance of <see cref="DependencyAttribute"/> with the given name.
        /// </summary>
        /// <param name="name">Name to use when resolving this dependency.</param>
        public DependencyAttribute(string name)
        {
            this.name = name;
        }