Example #1
0
        protected static List<InjectionTarget> GetInjectionTargets(Type type)
        {
            // see if we have it in the injection target cache
            if (InjectionTargetCache.ContainsKey(type))
            {
                return InjectionTargetCache[type];
            }

            // initialize injection targets
            var targets = new List<InjectionTarget>();

            // otherwise we will create injection targets
            var fieldInfos = type.GetFields();
            foreach (var fieldInfo in fieldInfos)
            {
                var attributes = fieldInfo.GetCustomAttributes(typeof(InjectAttribute), false);
                // if we have an inject attribute, this is considered a target
                if (attributes.Length > 0)
                {
                    var target = new InjectionTarget();
                    target.Type = fieldInfo.FieldType;
                    target.Property = fieldInfo.Name;
                    targets.Add(target);
                }
            }

            // fill the cache
            InjectionTargetCache[type] = targets;

            return targets;
        }
Example #2
0
 //_____________________________________________________________________
 //    Protected Methods
 //_____________________________________________________________________
 protected void ApplyToTarget(InjectionTarget target, Object value)
 {
     if (MappedValues.ContainsKey(target.Type))
     {
         var injectedValue = MappedValues[target.Type];
         var field = GetFieldInfo(value.GetType(), target.Property);
         if (field != null && injectedValue != null)
         {
             field.SetValue(value, injectedValue);
         }
     }
     else
     {
         throw new Exception("No rule for injecting " + target.Type.ToString() + " into " + value.GetType().ToString());
     }
 }