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); }
/// <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); }
/// <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); }
public string DetailsHeader() { var dynamicHeaderAttribute = ReflectedClass.GetAttribute <DynamicHeaderAttribute>(); return(dynamicHeaderAttribute != null ? !string.IsNullOrWhiteSpace(dynamicHeaderAttribute.DetailsHeader) ? dynamicHeaderAttribute.DetailsHeader : ReflectedClass.Name + " Details" : ReflectedClass.Name + " Details"); }
public string IndexHeader() { var dynamicHeaderAttribute = ReflectedClass.GetAttribute <DynamicHeaderAttribute>(); return(dynamicHeaderAttribute != null ? !string.IsNullOrWhiteSpace(dynamicHeaderAttribute.IndexHeader) ? dynamicHeaderAttribute.IndexHeader : ReflectedClass.Name + " List" : ReflectedClass.Name + " List"); }
public string DeleteHeader() { var dynamicHeaderAttribute = ReflectedClass.GetAttribute <DynamicHeaderAttribute>(); return(dynamicHeaderAttribute != null ? !string.IsNullOrWhiteSpace(dynamicHeaderAttribute.DeleteHeader) ? dynamicHeaderAttribute.DeleteHeader : "Delete " + ReflectedClass.Name : "Delete " + ReflectedClass.Name); }
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; }
//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); } }
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()); }
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); }
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); }
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); }
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; }
public bool ShowDelete() { return(ReflectedClass.GetAttribute <DynamicEntityAttribute>().ShowDelete); }
public string IndexProperties() { return(ReflectedClass.GetAttribute <DynamicEntityAttribute>().IndexProperties); }
//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); }
public bool ShowEdit() { return(ReflectedClass.GetAttribute <DynamicEntityAttribute>().ShowEdit); }
public string DetailsProperties() { return(ReflectedClass.GetAttribute <DynamicEntityAttribute>().DetailsProperties); }