public void InjectSingleParameterNotFound() { //new ClassWithConstructor(Class1 c1) var constructor = typeof(ClassWtihConstructors).GetConstructors().First(c => c.GetParameters().Length == 1); MappedConstructor mapped = MappedConstructor.Map(constructor, Mock.Of <IContainer>()); Assert.IsFalse(mapped.IsValid); }
public void GetEmptyConstructor() { var constructor = typeof(Class1).GetConstructors().First(); MappedConstructor mapped = MappedConstructor.Map(constructor, Mock.Of <IContainer>()); Assert.IsTrue(mapped.IsValid); Assert.IsInstanceOfType(mapped.Activate(), typeof(Class1)); }
public void InjectMultipleParameterNotAllFound() { IRegistratedService service; //new ClassWithConstructor(Class1 c1, Class2 c2) var constructor = typeof(ClassWtihConstructors).GetConstructors().First(c => c.GetParameters().Length == 2); var container = new Mock <IContainer>(); container.Setup(c => c.TryGetRegistration(typeof(Class1), out service)).Returns(true); MappedConstructor mapped = MappedConstructor.Map(constructor, container.Object); Assert.IsFalse(mapped.IsValid); }
public void InjectSingleParameterFound() { IRegistratedService service; //new ClassWithConstructor(Class1 c1) var constructor = typeof(ClassWtihConstructors).GetConstructors().First(c => c.GetParameters().Length == 1); var container = new Mock <IContainer>(); container.Setup(c => c.TryGetRegistration(typeof(Class1), out service)).Returns(true); MappedConstructor mapped = MappedConstructor.Map(constructor, container.Object); Assert.IsTrue(mapped.IsValid); Assert.IsInstanceOfType(mapped.Activate(), typeof(ClassWtihConstructors)); }
/// <summary> /// Maps a member from the type's definition to its constructed version. /// </summary> public IEntity MapMember(IEntity source) { if (source == null) { return(null); } if (_mappedMembers.ContainsKey(source)) { return(_mappedMembers[source]); } IEntity mapped = null; switch (source.EntityType) { case EntityType.Method: mapped = new MappedMethod(_typeSystemServices, ((ExternalMethod)source).MethodInfo, this); break; case EntityType.Constructor: mapped = new MappedConstructor(_typeSystemServices, ((ExternalConstructor)source).ConstructorInfo, this); break; case EntityType.Field: mapped = new MappedField(_typeSystemServices, ((ExternalField)source).FieldInfo, this); break; case EntityType.Property: mapped = new MappedProperty(_typeSystemServices, ((ExternalProperty)source).PropertyInfo, this); break; case EntityType.Type: mapped = MapType((IType)source); break; case EntityType.Event: mapped = new MappedEvent(_typeSystemServices, ((ExternalEvent)source).EventInfo, this); break; default: throw new ArgumentException( string.Format("Invalid entity type for mapping: {0}.", source.EntityType)); } _mappedMembers[source] = mapped; return(mapped); }