public DependentItem Register(string name, Func <object> factory, DependenceLifecycles depType = DependenceLifecycles.AlwaysNew) { var depItem = new DependentItem(this, name, factory, depType); this.NamedItems.Add(name, depItem); return(depItem); }
public DependentItem Register(string name, object value, Type valueType = null) { var depItem = new DependentItem(this, name, value, valueType); this.NamedItems.Add(name, depItem); return(depItem); }
public DependentItem(DependentItem container, string name, Func <object> factory, DependenceLifecycles lifecycle = DependenceLifecycles.AlwaysNew) { this._ContainerItem = container; this.NominalName = name; this.Lifecycle = lifecycle; if (lifecycle == DependenceLifecycles.AlwaysNew) { this._CreateInstance = factory; } else if (lifecycle == DependenceLifecycles.Constant) { object constantValue = null; bool valueInitted = false; this._CreateInstance = () => { if (valueInitted) { return(constantValue); } lock (this) { if (valueInitted) { return(constantValue); } constantValue = factory(); valueInitted = true; } return(constantValue); }; } }
public DependentItem FindDepedentItem(Type nominalType, string nominalName = null) { if (this._TypedItems == null) { return(null); } DependentItem result = null; if (this._TypedItems.TryGetValue(nominalType.GUID, out result)) { return(result); } if (nominalName != null && this._NamedItems != null) { if (this._NamedItems.TryGetValue(nominalName, out result)) { if (nominalType.IsAssignableFrom(result.SubstantiveType)) { return(result); } else { result = null; } } } if (this._ContainerItem != null) { return(this._ContainerItem.FindDepedentItem(nominalType, nominalName)); } return(null); }
public CreateInstanceGenerator(DependentItem item) { this.DependentItem = item; //this.InstanceExpr = Expression.Parameter(item.SubstantiveType,"inst"); //this.Locals.Add(this.InstanceExpr); }
public DependentItem GetDependentItem(Type nominalType) { if (this._TypedItems == null) { return(null); } DependentItem result = null; this._TypedItems.TryGetValue(nominalType.GUID, out result); return(result); }
public DependentItem GetDependentItem(string nominalName) { if (this._NamedItems == null) { return(null); } DependentItem result = null; this._NamedItems.TryGetValue(nominalName, out result); return(result); }
Expression GenNewExpression(DependentItem item, string varName) { var ctors = item.SubstantiveType.GetConstructors(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic); ConstructorInfo ctorInfo = null; ConstructorInfo noArgsCtorInfo = null; ParameterInfo[] args = null; var injCount = 0; foreach (var ctor in ctors) { var attr1 = ctor.GetCustomAttribute <NonInjectAttribute>(); if (attr1 != null) { continue; } var attr = ctor.GetCustomAttributes <InjectAttribute>(); if (attr != null) { ctorInfo = ctor; args = ctorInfo.GetParameters(); injCount++; } else { args = ctor.GetParameters(); if (args.Length == 0) { noArgsCtorInfo = ctor; } } } if (injCount > 1) { throw new InvalidProgramException("More than one Contructors were marked [Inject]"); } if (ctorInfo == null) { ctorInfo = noArgsCtorInfo; } if (ctorInfo == null) { throw new InvalidProgramException("No constructors were found for injection"); } if (args == null) { args = ctorInfo.GetParameters(); } return(GenNewExpression(item, varName, ctorInfo, args)); }
object GetConstValue(DependentItem item) { var value = item.CreateInstance(); if (value == null) { return(null); } if (value.GetType() == item.SubstantiveType) { return(value); } return(item.SubstantiveType.GetValue(value.ToString())); }
public DependentItem(DependentItem container, string name, object constantValue, Type valueType = null) { this._ContainerItem = container; this.NominalName = name; if (constantValue != null) { this.SubstantiveType = constantValue.GetType(); } else { this.SubstantiveType = valueType; } this.Lifecycle = DependenceLifecycles.Constant; this._CreateInstance = () => constantValue; }
Expression GenNewExpression(DependentItem currentItem, string varName, ConstructorInfo ctor, ParameterInfo[] args) { var local = Expression.Parameter(ctor.DeclaringType, varName ?? "inst"); if (this.InstanceExpr == null) { this.InstanceExpr = local; } this.TypedLocals.Add(ctor.DeclaringType.GUID, local); var paraExprs = new List <Expression>(); foreach (var paraInfo in args) { var type = paraInfo.ParameterType; ParameterExpression existedExpr = null; if (this.TypedLocals.TryGetValue(type.GUID, out existedExpr)) { paraExprs.Add(existedExpr); continue; } var name = paraInfo.Name; var depItem = currentItem.FindDepedentItem(type, name); if (depItem == null) { paraExprs.Add(Expression.Constant(paraInfo.ParameterType.GetDefaultValue(), paraInfo.ParameterType)); continue; } if (depItem.Lifecycle == DependenceLifecycles.Constant) { paraExprs.Add(Expression.Constant(GetConstValue(depItem), paraInfo.ParameterType)); } if (this.TypedLocals.TryGetValue(depItem.SubstantiveType.GUID, out existedExpr)) { paraExprs.Add(existedExpr); continue; } var subVarName = varName + "_" + paraInfo.Name; existedExpr = Expression.Parameter(paraInfo.ParameterType, varName + "_" + paraInfo.Name); paraExprs.Add(existedExpr); var instExpr = GenNewExpression(depItem, subVarName); this.Codes.Add(Expression.Assign(existedExpr, instExpr)); } var newExpr = Expression.New(ctor, paraExprs); this.Codes.Add(Expression.Assign(local, newExpr)); return(local); }
public DependentItem FindDepedentItem(string nominalName) { if (this._NamedItems == null) { return(null); } DependentItem result = null; if (this._NamedItems.TryGetValue(nominalName, out result)) { return(result); } if (this._ContainerItem != null) { return(this._ContainerItem.FindDepedentItem(nominalName)); } return(null); }