Note that there are some explanatory texts on larger screens.

plurals
  1. POUnity framework DependencyAttribute only works for public properties?
    text
    copied!<p>I was trying to clean up some accessability stuff in my code, and inadvertently broke Unity dependency injection. After a while I realized that I marked some public properties that I didn't really want exposed outside my DLLs to internal. Then I started getting exceptions.</p> <p>So it seems that using the [Dependency] attribute in Unity only works for public properties. I suppose that makes sense since the internal and private props wouldnt be visible to the Unity assembly, but <em>feels really dirty</em> to have a bunch of public properties that you <em>never</em> want anyone to set or be able to set, other than Unity.</p> <p>Is there a way to let unity set internal or private properties too?</p> <p>Here is the unit test I'd like to see pass. Currently only the public prop test passes:</p> <pre><code> [TestFixture] public class UnityFixture { [Test] public void UnityCanSetPublicDependency() { UnityContainer container = new UnityContainer(); container.RegisterType&lt;HasPublicDep, HasPublicDep&gt;(); container.RegisterType&lt;TheDep, TheDep&gt;(); var i = container.Resolve&lt;HasPublicDep&gt;(); Assert.IsNotNull(i); Assert.IsNotNull(i.dep); } [Test] public void UnityCanSetInternalDependency() { UnityContainer container = new UnityContainer(); container.RegisterType&lt;HasInternalDep, HasInternalDep&gt;(); container.RegisterType&lt;TheDep, TheDep&gt;(); var i = container.Resolve&lt;HasInternalDep&gt;(); Assert.IsNotNull(i); Assert.IsNotNull(i.dep); } [Test] public void UnityCanSetPrivateDependency() { UnityContainer container = new UnityContainer(); container.RegisterType&lt;HasPrivateDep, HasPrivateDep&gt;(); container.RegisterType&lt;TheDep, TheDep&gt;(); var i = container.Resolve&lt;HasPrivateDep&gt;(); Assert.IsNotNull(i); Assert.IsNotNull(i.depExposed); } } public class HasPublicDep { [Dependency] public TheDep dep { get; set; } } public class HasInternalDep { [Dependency] internal TheDep dep { get; set; } } public class HasPrivateDep { [Dependency] private TheDep dep { get; set; } public TheDep depExposed { get { return this.dep; } } } public class TheDep { } </code></pre> <hr> <p>Updated:</p> <p>I noticed the call stack to set the property passed from:</p> <pre><code>UnityCanSetPublicDependency() --&gt; Microsoft.Practices.Unity.dll --&gt; Microsoft.Practices.ObjectBuilder2.dll --&gt; HasPublicDep.TheDep.set() </code></pre> <p>So in an attempt to at least make the internal version work, I added these to my assembly's properties:</p> <pre><code>[assembly: InternalsVisibleTo("Microsoft.Practices.Unity")] [assembly: InternalsVisibleTo("Microsoft.Practices.Unity.Configuration")] [assembly: InternalsVisibleTo("Microsoft.Practices.ObjectBuilder2")] </code></pre> <p>However, no change. Unity/ObjectBuilder still won't set the internal property</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload