Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 794707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:21:26+00:00 2026-05-14T22:21:26+00:00

Register<IA, A>(); class B { public IA A {get;set;}} //container inject this property because

  • 0
Register<IA, A>();

class B { public IA A {get;set;}} 

//container inject this property because IA was registered

In autofac you can do

builder.RegisterType<A>().InjectProperties();

for this.

Is there any extension for unity to do this? Or may be extension which I can use as sample for implement this feature by myself?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:21:26+00:00Added an answer on May 14, 2026 at 10:21 pm

    This is not a simple task. To resolve it you should change Unity’s default behaviour of the property selector policy. You can change it. This is what I can propose

    namespace Microsoft.Practices.Unity.ObjectBuilder
    {
        public class ResolveBecouseWeCanPropertySelectorPolicy : PropertySelectorBase<DependencyResolutionAttribute>
        {
            private readonly IUnityContainer container;
    
            public ResolveBecouseWeCanPropertySelectorPolicy(IUnityContainer container) {
                this.container = container;
            }
    
            public override IEnumerable<SelectedProperty> SelectProperties(IBuilderContext context, IPolicyList resolverPolicyDestination) {
                Type t = context.BuildKey.Type;
                foreach (
                    PropertyInfo prop in
                        t.GetProperties(BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.Instance)) {
                    if (prop.GetIndexParameters().Length == 0 && 
                        prop.CanWrite
                        && prop.IsDefined(typeof (DependencyResolutionAttribute), false)
                        ) //Default behaviour
                    {
    
                        yield return CreateSelectedProperty(context, resolverPolicyDestination, prop);
    
                    }
                    //Alternate behaviour
                    else if (prop.GetIndexParameters().Length == 0 && 
                             prop.CanWrite && 
                        container.IsRegistered(prop.PropertyType)//don't mind about Dependency attribute if we can resolve it - we would
                        ) {
                                 {
                            yield return CreateSelectedPropertyForResolveBecouseWeCan(context, resolverPolicyDestination, prop);
                                 }
                    }
                }
            }
    
            private SelectedProperty CreateSelectedProperty(IBuilderContext context, IPolicyList resolverPolicyDestination, PropertyInfo property) {
                return DoCreateSelectedProperty(property, resolverPolicyDestination, CreateResolver(property), context);
            }
    
            private static SelectedProperty CreateSelectedPropertyForResolveBecouseWeCan(IBuilderContext context, IPolicyList resolverPolicyDestination, PropertyInfo property) {
                IDependencyResolverPolicy dependencyResolverPolicy = new DependencyAttribute().CreateResolver(property.PropertyType);
                return DoCreateSelectedProperty(property, resolverPolicyDestination, dependencyResolverPolicy, context);
            }
    
            private static SelectedProperty DoCreateSelectedProperty(PropertyInfo property, IPolicyList resolverPolicyDestination, IDependencyResolverPolicy dependencyResolverPolicy, IBuilderContext context) {
                string key = Guid.NewGuid().ToString();
                var result = new SelectedProperty(property, key);
                resolverPolicyDestination.Set( dependencyResolverPolicy, key);
                DependencyResolverTrackerPolicy.TrackKey(context.PersistentPolicies,
                                                         context.BuildKey,
                                                         key);
                return result;
            }
    
    
            protected override IDependencyResolverPolicy CreateResolver(PropertyInfo property)
            {
                var attributes =
                    property.GetCustomAttributes(typeof (DependencyResolutionAttribute), false)
                    .OfType<DependencyResolutionAttribute>()
                    .ToList();
    
                Debug.Assert(attributes.Count == 1);
    
                return attributes[0].CreateResolver(property.PropertyType);
            }
        }
    }
    

    This is modified DefaultUnityPropertySelectorPolicy wich you can find in source code of unity.

    After that you need to redefine default behaviour by using UnityContainerExtension mechanism.

      public class ResolveBecouseWeCanUnityContainerExtension : UnityContainerExtension {
            protected override void Initialize() {
                Context.Policies.SetDefault<IPropertySelectorPolicy>(
                       new ResolveBecouseWeCanPropertySelectorPolicy(Container));
            }
        }
    

    So now let’s imagine that you have following classes

    public interface IConcreteService {
            int Val { get; set; }
        }
        public class ConcreteService : IConcreteService {
    
            public int Val { get; set; }
    
            public ConcreteService() {
            }
        }
    
        public class B {
            //no attribute
            public IConcreteService ConcreteService { get; set; }
            public int SomeVal { get; set; }
        }
    

    so now this test should pass

    [TestMethod]
            public void TestMethod1() {
                var container = new UnityContainer();
                container.AddExtension(new ResolveBecouseWeCanUnityContainerExtension());
    
    
                container.RegisterType<IConcreteService, ConcreteService>();
    
    
                var b = new B();
                container.BuildUp(b);
                Assert.IsNotNull(b.ConcreteService);
            }
    

    But you should understand that this one would not

    [TestClass]
    public class UnitTest1 {
        [TestMethod]
        public void TestMethod1() {
            var container = new UnityContainer();
    
            container.RegisterType<IConcreteService, ConcreteService>();
    
    
            var b0 = new B();
            container.BuildUp(b0);
            Assert.IsNull(b0.ConcreteService);
    
            container.AddExtension(new ResolveBecouseWeCanUnityContainerExtension());
    
    
            var b = new B();
            container.BuildUp(b);
            Assert.IsNotNull(b.ConcreteService); //dies becouse plan cashed
        }
    }
    

    Update

    I’ve played a little bit more and found out how to make this test work

    [TestClass]
    public class UnitTest1 {
        [TestMethod]
        public void TestMethod1() {
            var container = new UnityContainer();
    
            container.RegisterType<IConcreteService, ConcreteService>();
    
            var b0 = new B();
            container.BuildUp(b0);
            Assert.IsNull(b0.ConcreteService);
    
            var b = new B();
            container.BuildUpAndResolveAllWeCan(b);
            Assert.IsNotNull(b.ConcreteService);
            //check if we have no broken something and that it will work second time
            var b1 = new B();
            container.BuildUp(b1);
            Assert.IsNull(b1.ConcreteService);
    
            var b2 = new B();
            container.BuildUpAndResolveAllWeCan(b2);
            Assert.IsNotNull(b2.ConcreteService);
        }
    }
    

    this is can be done in following way

    public static class UnityExtensions {
    
            public static T BuildUpAndResolveAllWeCan<T>(this IUnityContainer container, T existing) {
                return BuildUpAndResolveAllWeCan(container, existing, null) ;
            }
    
            public static T BuildUpAndResolveAllWeCan<T>(this IUnityContainer container, T existing, string name) {
                container.AddExtension(new ResolveBecouseWeCanUnityContainerExtension());
                //we are adding __BuildUpResolveAllWeCan__ to create new IBuildPlanPolicy for type T 
                //and IPropertySelectorPolicy is ResolveBecouseWeCanPropertySelectorPolicy
                //this will be cached so it will not hurt the performance
                var buildedUp = container.BuildUp(existing, (name ?? string.Empty) + "__BuildUpResolveAllWeCan__");
                container.AddExtension(new CleanUnityContainerExtension());
                return buildedUp;
            }
        }
    

    and adding clean up extension

     public class CleanUnityContainerExtension : UnityContainerExtension {
            protected override void Initialize() {
                Context.Policies.SetDefault<IPropertySelectorPolicy>(
                        new DefaultUnityPropertySelectorPolicy());
            }
        }
    

    You can download source file from here http://92.248.232.12/UnitTest1.zip

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that implements multiple interfaces. I would like to register these
using register_shutdown_function I can register code to execute at the end of a PHP
I register my two interfaces on application start as so:- container.Register(Component.For(typeof(IEntityIndexController)).ImplementedBy(typeof(SnippetController)).LifeStyle.Transient); container.Register(Component.For(typeof(ISnippetController)).ImplementedBy(typeof(SnippetController)).LifeStyle.Transient); Then when
I want to register to get notified of all Java changes in Eclipse. I
Do I need to register new extension types with Apple before I release an
I'm trying to register an externally hosted SQL 2000 server through Enterprise Manager which
I'm trying to register an atl service using ExeName.exe /service as described here: http://msdn.microsoft.com/en-us/library/74y2334x(VS.80).aspx
I'm attempting to register an anonymous function when a user clicks a cell in
I want to register a specific instance of an object for a type in
function register_contact ($person = array()) { $nogood = false; foreach ($person as $val) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.