private void InjectFields(IExecutable command) { var injectionType = typeof(InjectAttribute); var fieldsToInject = command .GetType() .GetFields(BindingFlags.NonPublic | BindingFlags.Instance) .Where(x => x.GetCustomAttributes().Any(f => f.GetType() == injectionType)); var interpreterFields = this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance); foreach (var field in fieldsToInject) { var interpreterField = interpreterFields.FirstOrDefault(x => x.FieldType == field.FieldType).GetValue(this); field.SetValue(command, interpreterField); } }
private IExecutable InjectDependencies(IExecutable currentCommand) { FieldInfo[] fields = currentCommand.GetType() .GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .Where(f => f.GetCustomAttributes <InjectAttribute>() != null).ToArray(); FieldInfo[] interpreterFields = this.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic); foreach (FieldInfo field in fields) { field.SetValue(currentCommand, interpreterFields .FirstOrDefault(f => f.FieldType == field.FieldType) .GetValue(this)); } return(currentCommand); }
private IExecutable InjectDependencies(IExecutable currentCommand) { FieldInfo[] commandFields = currentCommand .GetType() .GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .Where(f => f.GetCustomAttributes <InjectAttribute>() != null) .ToArray(); FieldInfo[] interpreterFields = this.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic); foreach (var field in commandFields) { FieldInfo interpreterField = interpreterFields.First(f => f.FieldType == field.FieldType); object valueToInject = interpreterField.GetValue(this); field.SetValue(currentCommand, valueToInject); } return(currentCommand); }
private void InjectDependencies(IExecutable command) { Type injectAttrType = typeof(InjectAttribute); var fieldsForInjection = command .GetType() .GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .Where(x => x.GetCustomAttributes().Any(a => a.GetType() == injectAttrType)); var interpeterFields = this.GetType() .GetFields(BindingFlags.Instance | BindingFlags.NonPublic); foreach (FieldInfo field in fieldsForInjection) { field .SetValue(command, interpeterFields.First(x => x.FieldType == field.FieldType) .GetValue(this)); } }
private void InjectDependancies(IExecutable command) { var type = typeof(InjectAttribute); var fields = command .GetType() .GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .Where(f => f.GetCustomAttributes().Any(a => a.GetType() == type)); var interpreterFields = this.GetType() .GetFields(BindingFlags.Instance | BindingFlags.NonPublic); foreach (var fieldForInjection in fields) { fieldForInjection .SetValue(command, interpreterFields .First(f => f.FieldType == fieldForInjection.FieldType) .GetValue(this)); } }
private IExecutable InjectDependencies(IExecutable command) { FieldInfo[] fieldsOfCommand = command.GetType() .GetFields(BindingFlags.Instance | BindingFlags.NonPublic); FieldInfo[] fieldsOfInterpreter = typeof(CommandInterpreter) .GetFields(BindingFlags.Instance | BindingFlags.NonPublic); foreach (FieldInfo field in fieldsOfCommand.Where(f => f.GetCustomAttribute <InjectAttribute>() != null)) { if (fieldsOfInterpreter.Any(x => x.FieldType == field.FieldType)) { field.SetValue(command, fieldsOfInterpreter.First(x => x.FieldType == field.FieldType) .GetValue(this)); } } return(command); }
public bool CanHandle(IExecutable command) { if (command == null) { throw new ArgumentNullException(nameof(command)); } try { return((bool)canHandleMethod.MakeGenericMethod(command.GetType()) .Invoke(this, new object[0])); } catch (TargetInvocationException ex) { // Rethrow the inner exception preserving stack trace. System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(ex.InnerException).Throw(); // Will never get here. throw ex.InnerException; } }
private IExecutable InjectDependencies(IExecutable command) { FieldInfo[] commandFields = command.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic); FieldInfo[] engineFields = this.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic); foreach (var field in commandFields) { var fieldAttribute = field.GetCustomAttribute(typeof(InjectAttribute)); if (fieldAttribute != null) { if (engineFields.Any(f => f.FieldType == field.FieldType)) { field.SetValue(command, engineFields.First(f => f.FieldType == field.FieldType).GetValue(this)); } } } return(command); }
private IExecutable InjectDependencies(IExecutable commandClassInstance) { FieldInfo[] commandFields = commandClassInstance.GetType() .GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .Where(f => f.GetCustomAttributes <InjectAttribute>() != null) .ToArray(); FieldInfo[] engineFields = this.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic); foreach (FieldInfo commandField in commandFields) { FieldInfo engineField = engineFields .First(f => f.FieldType == commandField.FieldType); object valueToInject = engineField.GetValue(this); commandField.SetValue(commandClassInstance, valueToInject); } return(commandClassInstance); }
private void InjectDependencies(IExecutable command) { FieldInfo[] fieldsOfCommand = command.GetType() .GetFields(BindingFlags.Instance | BindingFlags.NonPublic); FieldInfo[] fieldsOfInterpreter = typeof(CommandManager) .GetFields(BindingFlags.Instance | BindingFlags.NonPublic); foreach (var field in fieldsOfCommand) { var fieldAttribute = field.GetCustomAttribute(typeof(InjectAttribute)); if (fieldAttribute != null) { if (fieldsOfInterpreter.Any(x => x.FieldType == field.FieldType)) { field.SetValue(command, fieldsOfInterpreter.First(x => x.FieldType == field.FieldType) .GetValue(this)); } } } }
public IExecutable InterpretCommand(string[] data, string commandName) { var commandFullName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(commandName); Type commandType = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(t => t.Name == commandFullName); if (commandType == null) { throw new InvalidOperationException("Invalid command!"); } IExecutable command = (IExecutable)Activator.CreateInstance(commandType, new object[] { data }); var commandInjectFields = command.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic).Where(f => f.GetCustomAttributes <InjectAttribute>() != null).ToArray(); var interpreterFields = this.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic); foreach (var commandInjectField in commandInjectFields) { commandInjectField.SetValue(command, interpreterFields.First(f => f.FieldType == commandInjectField.FieldType).GetValue(this)); } return(command); }
private IExecutable InjectDependencies(IExecutable currentCommand) { var fieldsToInject = currentCommand .GetType() .GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .Where(f => f.GetCustomAttribute <InjectAttribute>() != null) .ToArray(); var interpreterFields = this // interpreter .GetType() .GetFields(BindingFlags.Instance | BindingFlags.NonPublic); foreach (FieldInfo fieldToInject in fieldsToInject) { var fieldValue = interpreterFields .First(f => f.FieldType == fieldToInject.FieldType) .GetValue(this); fieldToInject.SetValue(currentCommand, fieldValue); } return(currentCommand); }
private IExecutable InjectDependencies(IExecutable command) { var fields = command .GetType() .GetFields(BindingFlags.NonPublic | BindingFlags.Instance) .Where(f => f.GetCustomAttribute <InjectAttribute>() != null) .ToArray(); var commandFields = this .GetType() .GetFields(BindingFlags.NonPublic | BindingFlags.Instance); foreach (FieldInfo field in fields) { var fieldValue = commandFields .First(f => f.FieldType == field.FieldType) .GetValue(this); field.SetValue(command, fieldValue); } return(command); }
private IExecutable InjectDependencies(IExecutable currentCommand) { var commandFileds = currentCommand .GetType() .GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .Where(f => f.GetCustomAttributes <Inject>() != null) .ToArray(); var thisFileds = this .GetType() .GetFields(BindingFlags.Instance | BindingFlags.NonPublic); foreach (var fieldInfo in commandFileds) { var field = thisFileds .SingleOrDefault(f => f.FieldType == fieldInfo.FieldType) .GetValue(this); fieldInfo.SetValue(currentCommand, field); } return(currentCommand); }
private void InjectAttributes(IExecutable instance) { Type instanceType = instance.GetType(); var fields = instanceType .GetFields(BindingFlags.NonPublic | BindingFlags.Instance) .Where(f => f.GetCustomAttributes(false).Any(x => x.GetType() == typeof(InjectAttribute))); var currentTypeFields = this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance); foreach (FieldInfo field in fields) { foreach (var currentField in currentTypeFields) { var firstField = field.GetType(); var secondField = currentField.GetType(); if (field.FieldType == currentField.FieldType) { field.SetValue(instance, currentField.GetValue(this)); } } } }
private IExecutable InjectDependencies(IExecutable command) { FieldInfo[] commandFields = command.GetType() .GetFields(BindingFlags.Instance | BindingFlags.NonPublic); PropertyInfo[] dispatcherFields = typeof(UnitOfWork) .GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (var field in commandFields) { var fieldAttribute = field.GetCustomAttribute(typeof(InjectAttribute)); if (fieldAttribute != null) { if (dispatcherFields.Any(x => x.PropertyType == field.FieldType)) { field.SetValue(command, dispatcherFields.First(x => x.PropertyType == field.FieldType) .GetValue(this.UnitOfWork)); } } } return(command); }
private IExecutable InjectDependancy(IExecutable command) { var interpreterFields = this .GetType() .GetFields(BindingFlags.Instance | BindingFlags.NonPublic); var commandFields = command .GetType() .GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .Where(f => f.GetCustomAttribute <CommandInjectionAttribute>() != null) .ToArray(); foreach (var commandField in commandFields) { var injectValue = interpreterFields .First(f => f.FieldType == commandField.FieldType) .GetValue(this); commandField.SetValue(command, injectValue); } return(command); }
public void Run() { while (true) { string[] tokens = this.reader.ReadLine().Split(';'); IExecutable executable = this.commandInterpreter.InterpretCommand(tokens[0], tokens.Skip(1).ToArray()); var fields = executable.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance); if (fields.Any(f => f.FieldType == typeof(IRepository))) { fields.Single(f => f.FieldType == typeof(IRepository)) .SetValue(executable, this.repository); } if (fields.Any(f => f.FieldType == typeof(IWeaponFactory))) { fields.Single(f => f.FieldType == typeof(IWeaponFactory)) .SetValue(executable, this.weaponFactory); } if (fields.Any(f => f.FieldType == typeof(IGemFactory))) { fields.Single(f => f.FieldType == typeof(IGemFactory)) .SetValue(executable, this.gemFactory); } if (fields.Any(f => f.FieldType == typeof(IWriter))) { fields.Single(f => f.FieldType == typeof(IWriter)) .SetValue(executable, this.writer); } executable.Execute(); } }
public void ShouldReturnTypeName(IExecutable <ICustomExtension> testee) { string expectedName = testee.GetType().FullNameToString(); testee.Name.Should().Be(expectedName); }
public void Initialize(IExecutable obj) { var sequenceProgram = obj as SequenceProgram; if (sequenceProgram != null) { Initialize(sequenceProgram); } else { var eventSequence = obj as EventSequence; if (eventSequence != null) { Initialize(eventSequence); } else { var profile = obj as Profile; if (profile == null) { throw new Exception("Trying to initialize the engine with an unknown object type.\nType: " + obj.GetType()); } Initialize((Profile)obj); } } }
public void Initialize(IExecutable obj) { var sequenceProgram = obj as SequenceProgram; if (sequenceProgram != null) { Initialize(sequenceProgram); } else { var eventSequence = obj as EventSequence; if (eventSequence != null) { Initialize(eventSequence); } else { var profile = obj as Profile; if (profile == null) { throw new Exception("Trying to initialize the engine with an unknown object type.\nType: " + obj.GetType()); } Initialize((Profile) obj); } } }
private static bool hasIncommingEdges(IExecutable executable, IEnumerable <IExecutable> executables) { return(executables.Any(e => e.Dependencies != null && e.Dependencies.Contains(executable.GetType()))); }
public void ShouldReturnTypeName(IExecutable<ICustomExtension> testee) { string expectedName = testee.GetType().FullNameToString(); testee.Name.Should().Be(expectedName); }