Skip to content

Commit dd7e617

Browse files
committed
Fixed #88
1 parent 4090608 commit dd7e617

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

src/ObjectBuilder/BuildPlan/DynamicMethod/Creation/DynamicMethodConstructorStrategy.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ private IEnumerable<Expression> CreateNewBuildupSequence(DynamicBuildPlanGenerat
169169
yield return Expression.Call(null,
170170
SetCurrentOperationToInvokingConstructorMethod,
171171
Expression.Constant(signature),
172-
buildContext.ContextParameter);
172+
buildContext.ContextParameter,
173+
Expression.Constant(selectedConstructor.Constructor.DeclaringType));
173174

174175
yield return Expression.Assign(
175176
buildContext.GetExistingObjectExpression(),
@@ -194,7 +195,8 @@ private IEnumerable<Expression> BuildConstructionParameterExpressions(DynamicBui
194195
SetCurrentOperationToResolvingParameterMethod,
195196
Expression.Constant(constructionParameters[i].Name, typeof(string)),
196197
Expression.Constant(constructorSignature),
197-
buildContext.ContextParameter));
198+
buildContext.ContextParameter,
199+
Expression.Constant(selectedConstructor.Constructor.DeclaringType)));
198200
i++;
199201
}
200202
}
@@ -258,19 +260,17 @@ private static void GuardTypeIsNonPrimitive(IBuilderContext context)
258260
/// <summary>
259261
/// A helper method used by the generated IL to store the current operation in the build context.
260262
/// </summary>
261-
public static void SetCurrentOperationToResolvingParameter(string parameterName, string constructorSignature, IBuilderContext context)
263+
public static void SetCurrentOperationToResolvingParameter(string parameterName, string constructorSignature, IBuilderContext context, Type type)
262264
{
263-
(context ?? throw new ArgumentNullException(nameof(context))).CurrentOperation = new ConstructorArgumentResolveOperation(
264-
context.BuildKey.Type, constructorSignature, parameterName);
265+
context.CurrentOperation = new ConstructorArgumentResolveOperation(type, constructorSignature, parameterName);
265266
}
266267

267268
/// <summary>
268269
/// A helper method used by the generated IL to store the current operation in the build context.
269270
/// </summary>
270-
public static void SetCurrentOperationToInvokingConstructor(string constructorSignature, IBuilderContext context)
271+
public static void SetCurrentOperationToInvokingConstructor(string constructorSignature, IBuilderContext context, Type type)
271272
{
272-
(context ?? throw new ArgumentNullException(nameof(context))).CurrentOperation = new InvokingConstructorOperation(
273-
context.BuildKey.Type, constructorSignature);
273+
context.CurrentOperation = new InvokingConstructorOperation(type, constructorSignature);
274274
}
275275

276276
/// <summary>

tests/Unity.Tests/Issues/GitHub.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,41 @@ namespace Unity.Tests.Issues
1111
[TestClass]
1212
public class GitHubIssues
1313
{
14-
public interface IFoo { }
14+
public interface IFoo
15+
{
16+
string View { get; }
17+
}
1518

1619
public class Foo : IFoo
1720
{
18-
public Foo(string view) { }
21+
public string View { get; }
22+
23+
public Foo(string view)
24+
{
25+
View = view;
26+
}
1927
}
2028

2129
[TestMethod]
2230
public void unitycontainer_container_88()
2331
{
32+
var str1 = "s1";
33+
var str2 = "s2";
34+
2435
var ioc = new UnityContainer();
2536
ioc.RegisterType<IFoo, Foo>(new HierarchicalLifetimeManager());
2637

27-
var value1 = ioc.CreateChildContainer().Resolve<IFoo>(new Resolution.ParameterOverride("view", "qq").OnType<Foo>());
28-
var value2 = ioc.CreateChildContainer().Resolve<IFoo>(new Resolution.ParameterOverride("view", "qq").OnType<Foo>());
38+
var ch1 = ioc.CreateChildContainer();
39+
var ch2 = ioc.CreateChildContainer();
40+
41+
var value1 = ch1.Resolve<IFoo>(new Resolution.ParameterOverride("view", str1).OnType<Foo>());
42+
var value2 = ch2.Resolve<IFoo>(new Resolution.ParameterOverride("view", str2).OnType<Foo>());
43+
44+
Assert.IsNotNull(value1);
45+
Assert.IsNotNull(value2);
46+
47+
Assert.AreEqual(value1.View, str1);
48+
Assert.AreEqual(value2.View, str2);
2949
}
3050

3151
[TestMethod]

0 commit comments

Comments
 (0)