Beispiel #1
0
        private static void Inject(IEnumerable <MemberInfo> memberInfoArray, IInjectingTarget target)
        {
            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 (attr is InjectAttribute)
                    {
                        //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
                        //If this throws an exception there is probably a child Inject prop that is not registered
                        object 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?"));
                        }
                        AssignValueToTarget(target, memberInfo, valueToInject);
                    }
                }
            }
        }
Beispiel #2
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);

                    }
                }
            }
        }
Beispiel #3
0
        static void Inject(MemberInfo[] memberInfoArray, IInjectingTarget target)
        {
            foreach (var memberInfo in memberInfoArray)
            {
                object[] attrs = GetAttributes(target, memberInfo);

                foreach (Attribute attr in attrs)
                {
                    if (attr is InjectAttribute)
                    {
                        //retrieve injectable from DI
                        var mi = typeof(DI).GetMethod("Get", BindingFlags.Static | BindingFlags.Public);

                        //getting method to get value to inject
                        MethodInfo methodInfo = GetMethodInfo(mi, memberInfo);



                        //Checking if value was found
                        var 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);
                        }
                    }
                }
            }
        }
Beispiel #4
0
 private static void AssignValueToTarget(IInjectingTarget target, MemberInfo memberInfo, object valueToInject)
 {
     if (memberInfo is FieldInfo) {
         (memberInfo as FieldInfo).SetValue (target, valueToInject);
     }
     if (memberInfo is PropertyInfo) {
         (memberInfo as PropertyInfo).SetValue (target, valueToInject, null);
     }
 }
 static void AssignValueToTarget(IInjectingTarget target, MemberInfo memberInfo, object valueToInject)
 {
     if (memberInfo is System.Reflection.FieldInfo) {
         (memberInfo as System.Reflection.FieldInfo).SetValue (target, valueToInject);
     }
     if (memberInfo is System.Reflection.PropertyInfo) {
         (memberInfo as System.Reflection.PropertyInfo).SetValue (target, valueToInject, null);
     }
 }
Beispiel #6
0
 private static IEnumerable<object> GetAttributes(IInjectingTarget target, MemberInfo memberInfo)
 {
     if (memberInfo is FieldInfo) {
         return target.GetType ().GetField (memberInfo.Name).GetCustomAttributes (false);
     }
     if (memberInfo is PropertyInfo) {
         return target.GetType ().GetProperty (memberInfo.Name).GetCustomAttributes (false);
     }
     throw (new ArgumentException ("Inject tag was places on a member type injection doesn't support"));
 }
Beispiel #7
0
 private static void AssignValueToTarget(IInjectingTarget target, MemberInfo memberInfo, object valueToInject)
 {
     if (memberInfo is FieldInfo)
     {
         (memberInfo as FieldInfo).SetValue(target, valueToInject);
     }
     if (memberInfo is PropertyInfo)
     {
         (memberInfo as PropertyInfo).SetValue(target, valueToInject, null);
     }
 }
Beispiel #8
0
 static void AssignValueToTarget(IInjectingTarget target, MemberInfo memberInfo, object valueToInject)
 {
     if (memberInfo is System.Reflection.FieldInfo)
     {
         (memberInfo as System.Reflection.FieldInfo).SetValue(target, valueToInject);
     }
     if (memberInfo is System.Reflection.PropertyInfo)
     {
         (memberInfo as System.Reflection.PropertyInfo).SetValue(target, valueToInject, null);
     }
 }
Beispiel #9
0
 private static IEnumerable <object> GetAttributes(IInjectingTarget target, MemberInfo memberInfo)
 {
     if (memberInfo is FieldInfo)
     {
         return(target.GetType().GetField(memberInfo.Name).GetCustomAttributes(false));
     }
     if (memberInfo is PropertyInfo)
     {
         return(target.GetType().GetProperty(memberInfo.Name).GetCustomAttributes(false));
     }
     throw (new ArgumentException("Inject tag was places on a member type injection doesn't support"));
 }
Beispiel #10
0
 public static void InjectProps(IInjectingTarget target, InjectionMap map = null)
 {
     Inject (target.GetType ().GetProperties (), target, map);
     Inject (target.GetType ().GetFields (), target, map);
 }
 public static void InjectProps(IInjectingTarget target)
 {
     Inject (target.GetType ().GetProperties (), target);
     Inject (target.GetType ().GetFields (), target);
 }
Beispiel #12
0
 public static void InjectProps(IInjectingTarget target)
 {
     Inject(target.GetType().GetProperties(), target);
     Inject(target.GetType().GetFields(), target);
 }
 public static void InjectProps(IInjectingTarget target, InjectionMap map = null)
 {
     Inject(target.GetType().GetProperties(), target, map);
     Inject(target.GetType().GetFields(), target, map);
 }