Ejemplo n.º 1
0
        public static TypeInjectInfo GetTypeInjectInfo(Type type)
        {
            TypeInjectInfo info;

            if (s_cachedTypeInfo.TryGetValue(type, out info) == false)
            {
                info = new TypeInjectInfo(GetMethodInjectInfo(type));
                s_cachedTypeInfo.Add(type, info);
            }

            return(info);
        }
Ejemplo n.º 2
0
        public void Inject(object obj)
        {
            if (obj != null)
            {
                // get info
                Type           objType  = obj.GetType();
                TypeInjectInfo typeInfo = TypeAnalyzer.GetTypeInjectInfo(objType);

                // inject
                InjectMethods(obj, typeInfo);
                InjectFields(obj, typeInfo);
            }
        }
Ejemplo n.º 3
0
        public void Inject(object obj)
        {
            if (obj != null)
            {
                Type           objType  = obj.GetType();
                TypeInjectInfo typeInfo = TypeAnalyzer.GetTypeInjectInfo(objType);

                // inject methods
                foreach (MethodInjectInfo methodInjectInfo in typeInfo.MethodInfo)
                {
                    // get desired arguments to inject
                    List <object> methodArguments = new List <object>();

                    foreach (ParamInjectInfo paramInjectInfo in methodInjectInfo.ParamInfo)
                    {
                        object instance;
                        int    id = (paramInjectInfo.ID.HasValue == false)? c_defaultID : paramInjectInfo.ID.Value;

                        if (TryGet(paramInjectInfo.Type, out instance, id))
                        {
                            methodArguments.Add(instance);
                        }
                        else
                        {
                            if (paramInjectInfo.IsOptional == false)
                            {
                                throw new InvalidOperationException(string.Format("No binding of type '{0}' with ID '{1}' exists for attempted injection '{2}'",
                                                                                  paramInjectInfo.Type,
                                                                                  id,
                                                                                  methodInjectInfo.MethodInfo.Name));
                            }

                            methodArguments.Add(null);
                        }
                    }

                    methodInjectInfo.MethodInfo.Invoke(obj, methodArguments.ToArray());
                }
            }
        }