MemberExpressionFactory
class MemberExpressionFactory
using Stashbox.Exceptions;
using Stashbox.Registration;
using Stashbox.Resolution;
using Stashbox.Resolution.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace Stashbox.Expressions
{
internal class MemberExpressionFactory
{
public IEnumerable<Expression> GetMemberExpressions(IEnumerable<MemberInfo> members, RegistrationContext registrationContext, ResolutionContext resolutionContext, Expression instance)
{
return from member in members
let expression = GetMemberExpression(member, instance.Type, registrationContext, resolutionContext)
where expression != null
select instance.Member(member).AssignTo(expression);
}
public IEnumerable<MemberBinding> GetMemberBindings(IEnumerable<MemberInfo> members, RegistrationContext registrationContext, ResolutionContext resolutionContext, Type implementationType)
{
return from member in members
let expression = GetMemberExpression(member, implementationType, registrationContext, resolutionContext)
where expression != null
select member.AssignTo(expression);
}
private Expression GetMemberExpression(MemberInfo member, Type implementationType, RegistrationContext registrationContext, ResolutionContext resolutionContext)
{
TypeInformation typeInformation = member.AsTypeInformation(implementationType, registrationContext, resolutionContext.CurrentContainerContext.ContainerConfiguration);
Expression expression = registrationContext.InjectionParameters.SelectInjectionParameterOrDefault(typeInformation);
if (expression != null)
return expression;
Expression expression2 = resolutionContext.ResolutionStrategy.BuildExpressionForType(resolutionContext, typeInformation);
if (expression2 != null) {
ConstantExpression constantExpression = expression2 as ConstantExpression;
if (constantExpression != null && constantExpression.Value == null)
return null;
} else if (!resolutionContext.NullResultAllowed) {
string text = (member is PropertyInfo) ? "property" : "field";
throw new ResolutionFailedException(typeInformation.ParentType, "Unresolvable " + text + ": (" + typeInformation.Type.FullName + ")" + typeInformation.ParameterOrMemberName + ".", null);
}
return expression2;
}
}
}