Exemplo n.º 1
0
        static void Inject(MemberInfo[] memberInfoArray, IInjectingTarget target, InjectionMap map)
        {
            foreach (var memberInfo in memberInfoArray)
            {
                IEnumerable <object> attrs = GetAttributes(target, memberInfo);

                //TODO: Look into adding a warning if an inject prop is private or protected

                foreach (Attribute attr in attrs)
                {
                    if (map == null || !map.HasTypeMapped(GetMemberInfoType(memberInfo)))
                    {
                        //We get the reference declaration to DI's Get method
                        var mi = typeof(DI).GetMethod("Get", BindingFlags.Static | BindingFlags.Public);

                        //getting DI.Get method so we can invoke it to get the value
                        //mi = DI.Get
                        //memberInfo = value type we want from DI.Get
                        MethodInfo methodInfo = GetMethodInfo(mi, memberInfo);


                        //Checking if value was found
                        object valueToInject;
                        //If this throws an exception there is probably a child Inject prop that is not registered
                        valueToInject = methodInfo.Invoke(null, null);

                        if (valueToInject == null)
                        {
                            throw(new ArgumentException("Inject target type was not found. Did you forget to register it with DI?"));
                        }
                        else
                        {
                            AssignValueToTarget(target, memberInfo, valueToInject);
                        }
                    }
                    else
                    {
                        var valueToAssign = map.Get(GetMemberInfoType(memberInfo));
                        AssignValueToTarget(target, memberInfo, valueToAssign);
                    }
                }
            }
        }
Exemplo n.º 2
0
 public static void InjectProps(IInjectingTarget target, InjectionMap map = null)
 {
     Inject(target.GetType().GetProperties(), target, map);
     Inject(target.GetType().GetFields(), target, map);
 }