Fix parameter injection

This commit is contained in:
Antony Male 2014-02-18 12:26:08 +00:00
parent c7ea379071
commit 374f5d9049
2 changed files with 16 additions and 17 deletions

View File

@ -857,23 +857,20 @@ namespace Stylet
var creator = Expression.New(ctor, ctorParams); var creator = Expression.New(ctor, ctorParams);
var assignment = Expression.Assign(instanceVar, creator); var assignment = Expression.Assign(instanceVar, creator);
var buildUpExpression = container.GetBuilderUpper(this.Type).GetExpression(container, assignment); var buildUpExpression = container.GetBuilderUpper(this.Type).GetExpression(container, instanceVar);
Expression completeExpression; // We always start with:
// var instance = new Class(.....)
// If there's no buildUp, avoid creating the block expression after all - it hurts runtime performance // instance.Property1 = new ....
if (buildUpExpression == Expression.Empty()) // instance.Property2 = new ....
{ var blockItems = new List<Expression>() { assignment, buildUpExpression };
completeExpression = creator; // If it implements IInjectionAware, follow that up with:
} // instance.ParametersInjected
else if (typeof(IInjectionAware).IsAssignableFrom(this.Type))
{ blockItems.Add(Expression.Call(instanceVar, typeof(IInjectionAware).GetMethod("ParametersInjected")));
var blockItems = new List<Expression>() { assignment, buildUpExpression }; // Final appearance of instanceVar, as this sets the return value of the block
if (typeof(IInjectionAware).IsAssignableFrom(this.Type)) blockItems.Add(instanceVar);
blockItems.Add(Expression.Call(assignment, typeof(IInjectionAware).GetMethod("ParametersInjected"))); var completeExpression = Expression.Block(new[] { instanceVar }, blockItems);
blockItems.Add(instanceVar); // Final appearance of instanceVar, as this sets the return value of the block
completeExpression = Expression.Block(new[] { instanceVar }, blockItems);
}
this.creationExpression = completeExpression; this.creationExpression = completeExpression;
return completeExpression; return completeExpression;
@ -974,7 +971,9 @@ namespace Stylet
var parameterExpression = Expression.Parameter(typeof(object), "inputParameter"); var parameterExpression = Expression.Parameter(typeof(object), "inputParameter");
var typedParameterExpression = Expression.Convert(parameterExpression, this.type); var typedParameterExpression = Expression.Convert(parameterExpression, this.type);
var expression = this.GetExpression(container, typedParameterExpression);
this.implementor = Expression.Lambda<Action<object>>(this.GetExpression(container, typedParameterExpression), parameterExpression).Compile(); this.implementor = Expression.Lambda<Action<object>>(this.GetExpression(container, typedParameterExpression), parameterExpression).Compile();
return this.implementor; return this.implementor;
} }
} }

View File

@ -53,7 +53,7 @@ namespace StyletUnitTests
public C1 C1 = null; public C1 C1 = null;
} }
class Subject6 class Subject6 : IInjectionAware
{ {
[Inject] [Inject]
public C1 C1 = null; public C1 C1 = null;