question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Injecting into private field of a base class doesn't work

See original GitHub issue

If I have a private field with the Inject attribute in a base class, it won’t be injected upon resolving.

public class DataManager { }

public class BaseData
{
    [Inject] private DataManager _dataManager;
}

public class LevelData : BaseData { }

...

var builder = new ContainerBuilder();
            
builder.Register<DataManager>(Lifetime.Scoped).AsSelf();
builder.Register<LevelData>(Lifetime.Scoped).AsSelf();

var container = builder.Build();

var levelData = container.Resolve<LevelData>();

At this point, levelData._dataManager is null. But if I change private to protected, the field will be injected. Is this the desired behavior?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kawai125commented, Mar 27, 2022

The behavior is due to the C# language specification.

VContainer’s field injection is done in the generated extension methods, but C# extension methods cannot access private variables in the type they are extending.

Constructor Injection or Method Injection can be used for Injection to private variables.

0reactions
hadashiAcommented, Apr 24, 2023

Fixed in #468 #505

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to inject a private field in a Component class while ...
First lets take a look at your plans and bust some myths/misunderstandings. Plan A: Using constructor to initiate the private field.
Read more >
Avoid private field dependency injection - here is why
Dependency injection is a great way of decoupling system components, assuming they are written using a contract-implementation approach.
Read more >
When extending a class, use an injected object of that class
The problem in your second snippet is that the child should not extend but implement the parent interface. You may then very well...
Read more >
ASP.NET Core Blazor dependency injection
This article explains how Blazor apps can inject services into components. Dependency injection (DI) is a technique for accessing services ...
Read more >
Java static code analysis: Private fields only used as local ...
When the value of a private field is always assigned to in a class' methods before being read, then it is not being...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found