private static void OnMessageReceived(object server, MessageEventArgs args) { if (args.Session.Data == null) { var handlers = new object[] { new Handlers() }; args.Session.Data = Autowire.CreateHandler(handlers); } ((EventHandler <MessageEventArgs>)args.Session.Data)(server, args); }
public object GetBeanById(string BeanId) { if (!BeansWithId.ContainsKey(BeanId)) { throw new BeanException($"bean with ID {BeanId} not found - aborting"); } if (Singletons.ContainsKey(BeanId)) { return(Singletons[BeanId]); } Type t = Type.GetType(BeansWithId[BeanId]); object o = null; try { o = Activator.CreateInstance(t); } catch (MissingMethodException e) { Console.WriteLine($"Error creating the bean ID {BeanId} - empty constructor missing in class!"); throw new BeanCreationException($"Error creating the bean ID {BeanId} - empty constructor missing in class! original exception: {e.Message}"); } foreach (var f in t.GetFields()) { foreach (var a in f.GetCustomAttributes()) { if (a.GetType() == typeof(Autowire)) { //Console.WriteLine($"'GetBean() name: {name}, fullname {t.FullName}, found fields with Attr 'Autowire'"); // Console.WriteLine($"'GetBean() name: {name}, fullname {t.FullName}, fieldname: '{f.Name}', fieldtype: {f.FieldType.ToString()}"); var interfaceType = f.FieldType.ToString(); List <string> interfaceImplementations = injectables[interfaceType]; if (interfaceImplementations == null) { throw new BeanNoImplementationFound($"could not fina an implemention of interface {interfaceType} while constructing bean ID {BeanId} for field {f.Name}"); } string classFullName = null; if (interfaceImplementations.Count > 1) { Autowire aw = a as Autowire; if (aw == null) { throw new BeanException($"could not typecast attribute to 'Autowire': "); } if (aw.Name == null) { string s = string.Join(",", interfaceImplementations); throw new BeanMultipleImplementationsFoundException($"for field {f.Name}. multiple implementation for interface {interfaceType} found: '{s}', but no implementation specified with 'Name' selector "); } if (!beans.ContainsKey(aw.Name)) { throw new BeanNoImplementationFound($"for field {f.Name}. implementation of interface {interfaceType} specified in 'Name' not found. classname {aw.Name}"); } classFullName = beans[aw.Name]; } else { classFullName = interfaceImplementations[0]; } Type ft = Type.GetType(classFullName); object b = null; if (Singletons.ContainsKey(classFullName)) { b = Singletons[classFullName]; } else { try { b = Activator.CreateInstance(ft); } catch (Exception e) { Console.WriteLine($"error creating object classname fullname: {classFullName}"); throw new BeanCreationException($"error creating object , fullname: {classFullName}, {e.Message}"); } } f.SetValue(o, b); } } } return(o); }
protected void Application_Start(object sender, EventArgs e) { Autowire.ConfigureWith(new ActivatorContainer()); }