Skip to content
Browse files

Bug fix

  • Loading branch information...
1 parent c2011c4 commit 9e0fcf425d9a1bc15e3bd15b14255380910f1e67 Jamie Briant committed Jan 26, 2012
View
37 BinaryFinery.IOC.Runtime/Build/ContextFactory.cs
@@ -393,7 +393,7 @@ private PropertyInfo PropertyForType(Type parameterType, Type startingContext)
{
return propertyInfo;
}
- ConstructionNode node = ImplementationTypeForProperty(propertyInfo.Name);
+ var node = ImplementationTypeForProperty(propertyInfo.Name);
if (node.ImplementationType == parameterType || parameterType.IsAssignableFrom(node.ImplementationType))
{
return propertyInfo;
@@ -437,7 +437,7 @@ internal TContext Create<TContext>()
BaseContextImpl impl = (BaseContextImpl) result;
impl.SetFactory(this);
// now have to check for eager implementations
- CreateEagerImplementations(custom, impl);
+ CreateEagerImplementations(custom);
externalContext = (TContext) result;
}
return (TContext) externalContext;
@@ -452,16 +452,25 @@ internal IContext CreateBasic<TContext>()
BaseContextImpl impl = (BaseContextImpl)result;
impl.SetFactory(this);
// now have to check for eager implementations
- CreateEagerImplementations(typeof(TContext), impl);
+ CreateEagerImplementations(typeof(TContext));
externalContext = (IContext)result;
}
return externalContext;
}
- private void CreateEagerImplementations(Type type, BaseContextImpl impl)
+ private void CreateEagerImplementations(Type type)
{
- var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
-
+ IEnumerable<PropertyInfo> props;
+ if (type.IsInterface)
+ {
+ var buildProps = new Dictionary<string, PropertyInfo>();
+ AddProps(buildProps, type);
+ props = buildProps.Values;
+ }
+ else
+ {
+ props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
+ }
foreach (var info in props)
{
var node = ImplementationTypeForPropertyBuilder(info.Name);
@@ -478,5 +487,21 @@ private void CreateEagerImplementations(Type type, BaseContextImpl impl)
}
CompleteBuild();
}
+
+ private void AddProps(Dictionary<string, PropertyInfo> buildProps, Type type)
+ {
+ var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
+ foreach (var propertyInfo in props)
+ {
+ if (!buildProps.ContainsKey(propertyInfo.Name))
+ {
+ buildProps[propertyInfo.Name] = propertyInfo;
+ }
+ }
+ foreach (var iface in type.GetInterfaces())
+ {
+ AddProps(buildProps,iface);
+ }
+ }
}
}
View
14 BinaryFinery.IOC.UnitTests/Inputs/Basics.cs
@@ -51,5 +51,19 @@ public interface IPointlessDerivedImplementationContext : IImplementationTestCon
IFoo FooP { get; }
}
+ public interface IBaseOne : IContext
+ {
+ Bar Bar { get; }
+ }
+
+ public interface IBaseTwo : IContext
+ {
+ Foo Foo { get; }
+ }
+
+ public interface ICompound : IBaseOne, IBaseTwo
+ {
+
+ }
}
View
10 BinaryFinery.IOC.UnitTests/Tests/Building/TestContextManagement.cs
@@ -41,5 +41,15 @@ public void TestCanCreateBasicContext()
var foo = context.Get<IFoo>("FooP");
Assert.That(foo,Is.InstanceOfType(typeof(Foo)));
}
+ [Test]
+ public void TestCanCreateBasicContextWithAllInterfaces()
+ {
+ ContextManager cm = ContextSystem.ManagerForTestingIoCItselfWithoutTestingFlags;
+ var context = cm.CreateBasic<ICompound>();
+ var foo = context.Get<Foo>("Foo");
+ Assert.That(foo, Is.InstanceOfType(typeof(Foo)));
+ var bar = context.Get<Bar>("Bar");
+ Assert.That(bar, Is.InstanceOfType(typeof(Bar)));
+ }
}
}

0 comments on commit 9e0fcf4

Please sign in to comment.
Something went wrong with that request. Please try again.