Example #1
0
        public object Inject(object target, bool isAvoidDestroy = false)
        {
            failIf(binder == null, "Attempt to inject into Injector without a Binder",
                   InjectionExceptionType.NO_BINDER);
            failIf(reflector == null, "Attempt to inject without a reflector", InjectionExceptionType.NO_REFLECTOR);
            failIf(target == null, "Attempt to inject into null instance", InjectionExceptionType.NULL_TARGET);

            //Some things can't be injected into. Bail out.
            var t = target.GetType();

            if (t.IsPrimitive || t == typeof(decimal) || t == typeof(string))
            {
                return(target);
            }

            ReflectedClass reflection = default;

#if !STRANGE_ANALYSIS
            reflection = reflector.Get(t);
#else
            StrangeDebugger.Measure(() => reflection = reflector.Get(t), null, target, "GetReflectedInfo");
#endif

            failIf(
                reflection == null, "Attempt to PostConstruct without a reflection",
                InjectionExceptionType.NULL_REFLECTION);

            if (reflection.Setters.Length > 0)
            {
#if !STRANGE_ANALYSIS
                performSetterInjection(target, reflection);
#else
                StrangeDebugger.Measure(() => performSetterInjection(target, reflection), null, target, "SetterInjection");
#endif
            }

            if (reflection.PostConstructor != null)
            {
#if !STRANGE_ANALYSIS
                postInject(target, reflection);
#else
                StrangeDebugger.Measure(() => postInject(target, reflection), null, target, "PostConstruct");
#endif
            }

            if (reflection.DeConstructor != null && !isAvoidDestroy)
            {
                _deconstructed.Add(new DeconstructBucket {
                    Target = target, Deconstruct = reflection.DeConstructor
                });
            }

            //exclude context to avoid dead loop. Dispose on context must be called manually
            if (target is IDisposable && !(target is IContext) && !isAvoidDestroy)
            {
                _disposed.Add((IDisposable)target);
            }

            return(target);
        }
Example #2
0
        /// <summary>
        /// Injects dependencies on an instance of an object.
        /// </summary>
        /// <param name="instance">The instance to have its dependencies resolved.</param>
        /// <param name="reflectedClass">The reflected class related to the <paramref name="instance"/>.</param>
        protected object Inject(object instance, ReflectedClass reflectedClass)
        {
            if (this.beforeInject != null)
            {
                this.beforeInject(this, ref instance, reflectedClass);
            }

            if (reflectedClass.fields.Length > 0)
            {
                this.InjectFields(instance, reflectedClass.fields);
            }

            if (reflectedClass.properties.Length > 0)
            {
                this.InjectProperties(instance, reflectedClass.properties);
            }

            //Call post constructors, if there's any.
            if (reflectedClass.postConstructors.Length > 0)
            {
                this.InjectPostConstructors(instance, reflectedClass.postConstructors);
            }

            if (this.afterInject != null)
            {
                this.afterInject(this, ref instance, reflectedClass);
            }

            return(instance);
        }
Example #3
0
        /// <summary>
        /// Injects dependencies on an instance of an object.
        /// </summary>
        /// <param name="instance">The instance to have its dependencies resolved.</param>
        /// <param name="reflectedClass">The reflected class related to the <paramref name="instance"/>.</param>
        protected object Inject(object instance, ReflectedClass reflectedClass)
        {
            if (this.beforeInject != null)
            {
                this.beforeInject(this, ref instance, reflectedClass);
            }

            if (reflectedClass.fields.Length > 0)
            {
                this.InjectFields(instance, reflectedClass.fields);
            }

            if (reflectedClass.properties.Length > 0)
            {
                this.InjectProperties(instance, reflectedClass.properties);
            }

            if (reflectedClass.methods.Length > 0)
            {
                this.InjectMethods(instance, reflectedClass.methods);
            }

            if (this.afterInject != null)
            {
                this.afterInject(this, ref instance, reflectedClass);
            }

            return(instance);
        }
Example #4
0
        public string DetailsHeader()
        {
            var dynamicHeaderAttribute = ReflectedClass.GetAttribute <DynamicHeaderAttribute>();

            return(dynamicHeaderAttribute != null ?
                   !string.IsNullOrWhiteSpace(dynamicHeaderAttribute.DetailsHeader) ? dynamicHeaderAttribute.DetailsHeader : ReflectedClass.Name + " Details"
                : ReflectedClass.Name + " Details");
        }
Example #5
0
        public string IndexHeader()
        {
            var dynamicHeaderAttribute = ReflectedClass.GetAttribute <DynamicHeaderAttribute>();

            return(dynamicHeaderAttribute != null ?
                   !string.IsNullOrWhiteSpace(dynamicHeaderAttribute.IndexHeader) ? dynamicHeaderAttribute.IndexHeader : ReflectedClass.Name + " List"
                : ReflectedClass.Name + " List");
        }
Example #6
0
        public string DeleteHeader()
        {
            var dynamicHeaderAttribute = ReflectedClass.GetAttribute <DynamicHeaderAttribute>();

            return(dynamicHeaderAttribute != null ?
                   !string.IsNullOrWhiteSpace(dynamicHeaderAttribute.DeleteHeader) ? dynamicHeaderAttribute.DeleteHeader : "Delete " + ReflectedClass.Name
                : "Delete " + ReflectedClass.Name);
        }
Example #7
0
    public static ReflectedClass Create(string InName, List<ReflectedProperty> InProperties)
    {
        Dictionary<string, ReflectedProperty> PropertyMap = new Dictionary<string, ReflectedProperty>();
        foreach (ReflectedProperty Property in InProperties)
            PropertyMap.Add(Property.Name, Property);

        ReflectedClass Class = new ReflectedClass(InName, PropertyMap);
        return Class;
    }
Example #8
0
        //Note that uninjection can only clean publicly settable points
        private void performUninjection(object target, ReflectedClass reflection)
        {
            var aa = reflection.Setters.Length;

            for (var a = 0; a < aa; a++)
            {
                var pair = reflection.Setters[a];
                pair.Value.SetValue(target, null, null);
            }
        }
Example #9
0
        public ICollection <string> InstanceIncludes()
        {
            var dynamicEntityAttribute = ReflectedClass.GetAttribute <DynamicEntityAttribute>();

            if (dynamicEntityAttribute.InstanceIncludes != null)
            {
                return(dynamicEntityAttribute.InstanceIncludes.SplitAndTrim().ToList());
            }
            return(DynamicPropertyMetadatas.Where(x => x.IsDynamicEntity()).Select(x => x.ReflectedProperty.Name).ToList());
        }
Example #10
0
        public DynamicPropertyMetadata KeyProperty()
        {
            var keyPropertyName = ReflectedClass.GetAttribute <DynamicEntityAttribute>().Key;
            var keyProperty     = DynamicPropertyMetadatas.SingleOrDefault(x => x.ReflectedProperty.Name == keyPropertyName);

            if (keyProperty == null)
            {
                throw new Exception("Could not find KeyValue for " + keyPropertyName + " and type " + TypeName());
            }
            return(keyProperty);
        }
Example #11
0
        public DynamicMenuInfo DynamicMenuInfo()
        {
            var dynamicMenuInfo = new DynamicMenuInfo();
            var dynamicMenuItemExcludeAttribute = ReflectedClass.GetAttribute <DynamicMenuItemExcludeAttribute>();
            var dynamicMenuItemAttribute        = ReflectedClass.GetAttribute <DynamicMenuItemAttribute>();

            dynamicMenuInfo.HasMenuItem      = dynamicMenuItemExcludeAttribute == null;
            dynamicMenuInfo.MenuItemCategory = dynamicMenuItemAttribute == null?_namingConventionManager.DynamicMenuCategory() : dynamicMenuItemAttribute.CategoryName;

            dynamicMenuInfo.MenuItemDisplayName = dynamicMenuItemAttribute != null ? dynamicMenuItemAttribute.DisplayName : ReflectedClass.Name;
            return(dynamicMenuInfo);
        }
Example #12
0
        private object[] GetArgs(ReflectedClass reflection)
        {
            var parameterTypes = reflection.ConstructorParameters;
            var parameterNames = reflection.ConstructorParameterNames;

            var aa   = parameterTypes.Length;
            var args = new object [aa];

            for (var a = 0; a < aa; a++)
            {
                args[a] = getValueInjection(parameterTypes[a], parameterNames[a], reflection.Type, null);
            }

            return(args);
        }
Example #13
0
        private void performSetterInjection(object target, ReflectedClass reflection)
        {
            failIf(
                reflection.Setters.Length != reflection.SetterNames.Length,
                "Attempt to perform setter injection with mismatched names.\nThere must be exactly as many names as setters.",
                InjectionExceptionType.SETTER_NAME_MISMATCH);

            var aa = reflection.Setters.Length;

            for (var a = 0; a < aa; a++)
            {
                var pair  = reflection.Setters[a];
                var value = getValueInjection(pair.Key, reflection.SetterNames[a], target, pair.Value);
                injectValueIntoPoint(value, target, pair.Value);
            }
        }
 public ObjectProperty(string InName, ReflectedClass InClass)
     : base(InName)
 {
     Type = ReflectedType.CLASS;
     Class = InClass;
 }
Example #15
0
 public bool ShowDelete()
 {
     return(ReflectedClass.GetAttribute <DynamicEntityAttribute>().ShowDelete);
 }
Example #16
0
 public string IndexProperties()
 {
     return(ReflectedClass.GetAttribute <DynamicEntityAttribute>().IndexProperties);
 }
Example #17
0
 //https://codeblog.jonskeet.uk/2008/08/09/making-reflection-fly-and-exploring-delegates/
 //After injection, call any methods labelled with the [PostConstruct] tag
 private void postInject(object target, ReflectedClass reflection)
 {
     reflection.PostConstructor.Invoke(target, null);
 }
Example #18
0
 public bool ShowEdit()
 {
     return(ReflectedClass.GetAttribute <DynamicEntityAttribute>().ShowEdit);
 }
Example #19
0
 public string DetailsProperties()
 {
     return(ReflectedClass.GetAttribute <DynamicEntityAttribute>().DetailsProperties);
 }