依赖注入的先后顺序

构造函数注入=>属性注入=>方法注入


方法注入

containerBuilder.RegisterType<ApplePhone>O.As<IPhone>O)
.PropertiesAutowired(new CustomPropertySelectorO)//支持属性注入
.OnActivated(activa =>
IPower powerl= activa.Context.Resolve<IPower>O:
activa.Instance.Init123456678890(power1);
)



属性选择器

class CustomPropertySelector : IPropertySelector
    {
        public bool InjectProperty(PropertyInfo propertyInfo, object instance)
        {
            bool bResult = propertyInfo
                                .CustomAttributes
                                .Any(c => c.AttributeType == typeof(CusotmSelectAttribute));
            return bResult;
        }
    }
posted @ 2024-12-10 22:58  网络来者  阅读(11)  评论(0)    收藏  举报