mirror of https://github.com/AMT-Cheif/Stylet.git
Fix parameter injection
This commit is contained in:
parent
c7ea379071
commit
374f5d9049
|
@ -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 ....
|
||||||
{
|
|
||||||
completeExpression = creator;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var blockItems = new List<Expression>() { assignment, buildUpExpression };
|
var blockItems = new List<Expression>() { assignment, buildUpExpression };
|
||||||
|
// If it implements IInjectionAware, follow that up with:
|
||||||
|
// instance.ParametersInjected
|
||||||
if (typeof(IInjectionAware).IsAssignableFrom(this.Type))
|
if (typeof(IInjectionAware).IsAssignableFrom(this.Type))
|
||||||
blockItems.Add(Expression.Call(assignment, typeof(IInjectionAware).GetMethod("ParametersInjected")));
|
blockItems.Add(Expression.Call(instanceVar, typeof(IInjectionAware).GetMethod("ParametersInjected")));
|
||||||
blockItems.Add(instanceVar); // Final appearance of instanceVar, as this sets the return value of the block
|
// Final appearance of instanceVar, as this sets the return value of the block
|
||||||
completeExpression = Expression.Block(new[] { instanceVar }, blockItems);
|
blockItems.Add(instanceVar);
|
||||||
}
|
var 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
|
Loading…
Reference in New Issue