private object createWithMethod(BeanMetadata meta, object baseObject) { if (manager.GetBeanMetadata(meta.Name).IsCreated) // check by name { return(meta.Bean); } var parameters = meta.Parameters; var ctorParams = new List <object>(); foreach (var parameter in parameters) { // inject [Value] var valueAttr = parameter.GetCustomAttribute <ValueAttribute>(); if (valueAttr != null) { ctorParams.Add(valueAttr.Value); continue; } var types = getAssignableTypes(parameter.GetType()); Type certainType = null; if (types.Count == 1) // no multiply interface implemention { certainType = types[0]; } else // scan [Autowired] in parameters { var paraAttr = parameter.GetCustomAttribute <AutowiredAttribute>(); certainType = getTypeByAutowiredAttributeClassName(paraAttr); } if (manager.ContainsBean(certainType)) { ctorParams.Add(createBean(manager.GetBeanMetadata(certainType))); } } object bean; if (meta.IsClassConstructor) { bean = ((ConstructorInfo)meta.Constructor).Invoke(ctorParams.ToArray()); } else if (meta.IsMethodConstructor) { bean = ((MethodInfo)meta.Constructor).Invoke(baseObject, ctorParams.ToArray()); } else { throw new NotImplementedException(); } meta.Bean = bean; return(bean); }
public void AddBean(BeanMetadata bean) { if (!beans.ContainsKey(bean.Name)) { beans.Add(bean.Name, bean); } else { throw new NotImplementedException(); } }
public BeanManager() { var selfMeta = new BeanMetadata(typeof(BeanManager).GetConstructors()[0], null, this); beans.Add(selfMeta.Name, selfMeta); }
private object createBean(BeanMetadata meta) { if (manager.GetBeanMetadata(meta.Name).IsCreated) // check by name { return(meta.Bean); } object bean = null; if (meta.IsClassConstructor) // a class constructor { bean = createWithMethod(meta, null); } else // a bean method { var parentObject = createBean(manager.GetBeanMetadata(meta.ParentType)); bean = createWithMethod(meta, parentObject); } // scan [Autowired] / [Value] for properties var properties = meta.BeanType.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public); foreach (var property in properties) { // [Value] var propAttrValue = property.GetCustomAttribute <ValueAttribute>(); if (propAttrValue != null) { property.SetValue(bean, propAttrValue.Value); continue; } // [Autowired] var propAttrAutowired = property.GetCustomAttribute <AutowiredAttribute>(); if (propAttrAutowired != null) { var propInterfaceType = property.PropertyType; var propTypes = getAssignableTypes(propInterfaceType); Type propType; if (propTypes.Count == 1) { propType = propTypes[0]; } else { propType = getTypeByAutowiredAttributeClassName(propAttrAutowired); } var propBeanMeta = manager.GetBeanMetadata(propType); var propBean = createBean(propBeanMeta); property.SetValue(bean, propBean); } } // scan for fields var fields = meta.BeanType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public); foreach (var field in fields) { var fieldAttrValue = field.GetCustomAttribute <ValueAttribute>(); if (fieldAttrValue != null) { field.SetValue(bean, fieldAttrValue.Value); continue; } var fieldAttrAutowired = field.GetCustomAttribute <AutowiredAttribute>(); if (fieldAttrAutowired != null) { var fieldInterfaceType = field.FieldType; var fieldTypes = getAssignableTypes(fieldInterfaceType); Type fieldType; if (fieldTypes.Count == 1) { fieldType = fieldTypes[0]; } else { fieldType = getTypeByAutowiredAttributeClassName(fieldAttrAutowired); } var fieldBeanMeta = manager.GetBeanMetadata(fieldType); var fieldBean = createBean(fieldBeanMeta); field.SetValue(bean, fieldBean); } } return(bean); }