Skip to content

How to introduce generic interface param to constructor #52

Answered by paxhujing
paxhujing asked this question in Q&A
Discussion options

You must be logged in to vote
public class LoggerAttribute : TypeAspect
{
    [Introduce(WhenExists = OverrideStrategy.Ignore)]
    private readonly ILogger _logger;

    public override void BuildAspect(IAspectBuilder<INamedType> builder)
    {
        Type type = typeof(ILogger<>).MakeGenericType(builder.Target.ToType());
        var constructors = builder.Target.Constructors;
        foreach (var constructor in constructors)
        {
            builder.Advice.IntroduceParameter(
                constructor,
                "logger",
                type,
                TypedConstant.Default(type),
                pullAction: (p, c) =>
                    PullAction.IntroduceParameterAndPull(
                    …

Replies: 4 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@paxhujing
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@gfraiteur
Comment options

Answer selected by gfraiteur
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants