LightInject and automatic property instantiation The Next CEO of Stack OverflowHidden Features of C#?How do you give a C# Auto-Property a default value?LINQ's Distinct() on a particular propertyHow to get Unity's automatic injection to work on interface injected constructors?Dependency injection and named loggersConstructor Injection Alternatives (Castle Windsor)Castle Windsor won't inject Logger in a property!Passing parameters/properties with MEFWindsor - pulling Transient objects from the containerNLog: Is passing a logger around classes a good practice?
Visit to the USA with ESTA approved before trip to Iran
Only print output after finding pattern
When did Lisp start using symbols for arithmetic?
Rotate a column
What makes a siege story/plot interesting?
MAZDA 3 2006 (UK) - poor acceleration then takes off at 3250 revs
Why is Miller's case titled R (Miller)?
Customer Requests (Sometimes) Drive Me Bonkers!
Is HostGator storing my password in plaintext?
Why do professional authors make "consistency" mistakes? And how to avoid them?
How to write the block matrix in LaTex?
% symbol leads to superlong (forever?) compilations
How can I open an app using Terminal?
Text adventure game code
Grabbing quick drinks
How to be diplomatic in refusing to write code that breaches the privacy of our users
I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin
How do I get the green key off the shelf in the Dobby level of Lego Harry Potter 2?
Does the Brexit deal have to be agreed by both Houses?
WOW air has ceased operation, can I get my tickets refunded?
Why Were Madagascar and New Zealand Discovered So Late?
Solution of this Diophantine Equation
Describing a person. What needs to be mentioned?
If I blow insulation everywhere in my attic except the door trap, will heat escape through it?
LightInject and automatic property instantiation
The Next CEO of Stack OverflowHidden Features of C#?How do you give a C# Auto-Property a default value?LINQ's Distinct() on a particular propertyHow to get Unity's automatic injection to work on interface injected constructors?Dependency injection and named loggersConstructor Injection Alternatives (Castle Windsor)Castle Windsor won't inject Logger in a property!Passing parameters/properties with MEFWindsor - pulling Transient objects from the containerNLog: Is passing a logger around classes a good practice?
Some time ago I worked on a project that I THINK used LightInject. I no longer have access, so I can't just go look for myself. It seemed like once the ServiceContainer was instantiated, something triggered reflection across all assemblies, and any properties of a certain interface type were automatically instantiated. Something like this:
A C# class library that contains a logger class; the logger is what should be injected.
namespace Common
public interface ILogger void Log(string msg);
public class Logger : ILogger
public Logger()
public void Log(string msg) Console.WriteLine(msg);
A C# console app that references the class library. Some things that didn't seem to help are commented out.
namespace TestLightInject
class Program
private static ServiceContainer container;
static void Main(string[] args)
container = new ServiceContainer();
//container.EnableAnnotatedPropertyInjection();
container.Register<ILogger, Logger>();
//container.RegisterPropertyDependency<ILogger>((factory, propertyInfo) => new Logger());
var worker = new Worker();
worker.DoSomething();
public class Worker
//[Inject]
ILogger logger get; set; = null; // THIS IS THE PROPERTY THAT NEEDS TO BE SET
public Worker()
public void DoSomething() logger.Log("It works!");
I guess I could allow public access to the service container, and change the Worker ctor to something like
public Worker() logger = Program.container.GetInstance<ILogger>();
but it was simpler when any ILogger property was automatically instantiated.
Is there a way to do this with LightInject, or was it some other DI framework that did it? Or am I just imagining it all?
c# .net dependency-injection
add a comment |
Some time ago I worked on a project that I THINK used LightInject. I no longer have access, so I can't just go look for myself. It seemed like once the ServiceContainer was instantiated, something triggered reflection across all assemblies, and any properties of a certain interface type were automatically instantiated. Something like this:
A C# class library that contains a logger class; the logger is what should be injected.
namespace Common
public interface ILogger void Log(string msg);
public class Logger : ILogger
public Logger()
public void Log(string msg) Console.WriteLine(msg);
A C# console app that references the class library. Some things that didn't seem to help are commented out.
namespace TestLightInject
class Program
private static ServiceContainer container;
static void Main(string[] args)
container = new ServiceContainer();
//container.EnableAnnotatedPropertyInjection();
container.Register<ILogger, Logger>();
//container.RegisterPropertyDependency<ILogger>((factory, propertyInfo) => new Logger());
var worker = new Worker();
worker.DoSomething();
public class Worker
//[Inject]
ILogger logger get; set; = null; // THIS IS THE PROPERTY THAT NEEDS TO BE SET
public Worker()
public void DoSomething() logger.Log("It works!");
I guess I could allow public access to the service container, and change the Worker ctor to something like
public Worker() logger = Program.container.GetInstance<ILogger>();
but it was simpler when any ILogger property was automatically instantiated.
Is there a way to do this with LightInject, or was it some other DI framework that did it? Or am I just imagining it all?
c# .net dependency-injection
Be careful with that last line because that's using the service locator pattern, which can appear to be similar to dependency injection but definitely isn't. What you need to do read up on DI best practices and research libraries. SO doesn't do library / external resource recommendations, so do some research, pick a DI library, take a shot at it, and then ask a question if you run into a problem.
– madreflection
Mar 21 at 16:41
I'm always careful. But the whole point of this questions is to NOT do what's in that last line. I'm not doing DI for the sake of doing DI because it's cool, I'm doing it because I need some objects injected in certain places. DI is usually expressed in terms of ctor or method injection, which is often impractical for a project of significant size. Carry a reference down thru 15 call levels just so it can be injected in the object that needs it? Add ctor injectors to 500 or 1000 classes? Let's not be ridiculous. What I need is reflective injection....
– Qodex
Mar 22 at 14:40
So, yes, I did some research, I picked a DI library (LightInject), took a shot at it (multiple shots, actually), ran into a problem, and asked a question. In case the main question wasn't clear, it's this: is there a way to get LightInject to do reflective injection? It appeared that it was doing so in a project I no longer have access to. Extensive googling has not turned up an answer. I'm not looking for a recommendation, just an answer to the question, can library X do Y?
– Qodex
Mar 22 at 14:46
add a comment |
Some time ago I worked on a project that I THINK used LightInject. I no longer have access, so I can't just go look for myself. It seemed like once the ServiceContainer was instantiated, something triggered reflection across all assemblies, and any properties of a certain interface type were automatically instantiated. Something like this:
A C# class library that contains a logger class; the logger is what should be injected.
namespace Common
public interface ILogger void Log(string msg);
public class Logger : ILogger
public Logger()
public void Log(string msg) Console.WriteLine(msg);
A C# console app that references the class library. Some things that didn't seem to help are commented out.
namespace TestLightInject
class Program
private static ServiceContainer container;
static void Main(string[] args)
container = new ServiceContainer();
//container.EnableAnnotatedPropertyInjection();
container.Register<ILogger, Logger>();
//container.RegisterPropertyDependency<ILogger>((factory, propertyInfo) => new Logger());
var worker = new Worker();
worker.DoSomething();
public class Worker
//[Inject]
ILogger logger get; set; = null; // THIS IS THE PROPERTY THAT NEEDS TO BE SET
public Worker()
public void DoSomething() logger.Log("It works!");
I guess I could allow public access to the service container, and change the Worker ctor to something like
public Worker() logger = Program.container.GetInstance<ILogger>();
but it was simpler when any ILogger property was automatically instantiated.
Is there a way to do this with LightInject, or was it some other DI framework that did it? Or am I just imagining it all?
c# .net dependency-injection
Some time ago I worked on a project that I THINK used LightInject. I no longer have access, so I can't just go look for myself. It seemed like once the ServiceContainer was instantiated, something triggered reflection across all assemblies, and any properties of a certain interface type were automatically instantiated. Something like this:
A C# class library that contains a logger class; the logger is what should be injected.
namespace Common
public interface ILogger void Log(string msg);
public class Logger : ILogger
public Logger()
public void Log(string msg) Console.WriteLine(msg);
A C# console app that references the class library. Some things that didn't seem to help are commented out.
namespace TestLightInject
class Program
private static ServiceContainer container;
static void Main(string[] args)
container = new ServiceContainer();
//container.EnableAnnotatedPropertyInjection();
container.Register<ILogger, Logger>();
//container.RegisterPropertyDependency<ILogger>((factory, propertyInfo) => new Logger());
var worker = new Worker();
worker.DoSomething();
public class Worker
//[Inject]
ILogger logger get; set; = null; // THIS IS THE PROPERTY THAT NEEDS TO BE SET
public Worker()
public void DoSomething() logger.Log("It works!");
I guess I could allow public access to the service container, and change the Worker ctor to something like
public Worker() logger = Program.container.GetInstance<ILogger>();
but it was simpler when any ILogger property was automatically instantiated.
Is there a way to do this with LightInject, or was it some other DI framework that did it? Or am I just imagining it all?
c# .net dependency-injection
c# .net dependency-injection
asked Mar 21 at 16:30
QodexQodex
1068
1068
Be careful with that last line because that's using the service locator pattern, which can appear to be similar to dependency injection but definitely isn't. What you need to do read up on DI best practices and research libraries. SO doesn't do library / external resource recommendations, so do some research, pick a DI library, take a shot at it, and then ask a question if you run into a problem.
– madreflection
Mar 21 at 16:41
I'm always careful. But the whole point of this questions is to NOT do what's in that last line. I'm not doing DI for the sake of doing DI because it's cool, I'm doing it because I need some objects injected in certain places. DI is usually expressed in terms of ctor or method injection, which is often impractical for a project of significant size. Carry a reference down thru 15 call levels just so it can be injected in the object that needs it? Add ctor injectors to 500 or 1000 classes? Let's not be ridiculous. What I need is reflective injection....
– Qodex
Mar 22 at 14:40
So, yes, I did some research, I picked a DI library (LightInject), took a shot at it (multiple shots, actually), ran into a problem, and asked a question. In case the main question wasn't clear, it's this: is there a way to get LightInject to do reflective injection? It appeared that it was doing so in a project I no longer have access to. Extensive googling has not turned up an answer. I'm not looking for a recommendation, just an answer to the question, can library X do Y?
– Qodex
Mar 22 at 14:46
add a comment |
Be careful with that last line because that's using the service locator pattern, which can appear to be similar to dependency injection but definitely isn't. What you need to do read up on DI best practices and research libraries. SO doesn't do library / external resource recommendations, so do some research, pick a DI library, take a shot at it, and then ask a question if you run into a problem.
– madreflection
Mar 21 at 16:41
I'm always careful. But the whole point of this questions is to NOT do what's in that last line. I'm not doing DI for the sake of doing DI because it's cool, I'm doing it because I need some objects injected in certain places. DI is usually expressed in terms of ctor or method injection, which is often impractical for a project of significant size. Carry a reference down thru 15 call levels just so it can be injected in the object that needs it? Add ctor injectors to 500 or 1000 classes? Let's not be ridiculous. What I need is reflective injection....
– Qodex
Mar 22 at 14:40
So, yes, I did some research, I picked a DI library (LightInject), took a shot at it (multiple shots, actually), ran into a problem, and asked a question. In case the main question wasn't clear, it's this: is there a way to get LightInject to do reflective injection? It appeared that it was doing so in a project I no longer have access to. Extensive googling has not turned up an answer. I'm not looking for a recommendation, just an answer to the question, can library X do Y?
– Qodex
Mar 22 at 14:46
Be careful with that last line because that's using the service locator pattern, which can appear to be similar to dependency injection but definitely isn't. What you need to do read up on DI best practices and research libraries. SO doesn't do library / external resource recommendations, so do some research, pick a DI library, take a shot at it, and then ask a question if you run into a problem.
– madreflection
Mar 21 at 16:41
Be careful with that last line because that's using the service locator pattern, which can appear to be similar to dependency injection but definitely isn't. What you need to do read up on DI best practices and research libraries. SO doesn't do library / external resource recommendations, so do some research, pick a DI library, take a shot at it, and then ask a question if you run into a problem.
– madreflection
Mar 21 at 16:41
I'm always careful. But the whole point of this questions is to NOT do what's in that last line. I'm not doing DI for the sake of doing DI because it's cool, I'm doing it because I need some objects injected in certain places. DI is usually expressed in terms of ctor or method injection, which is often impractical for a project of significant size. Carry a reference down thru 15 call levels just so it can be injected in the object that needs it? Add ctor injectors to 500 or 1000 classes? Let's not be ridiculous. What I need is reflective injection....
– Qodex
Mar 22 at 14:40
I'm always careful. But the whole point of this questions is to NOT do what's in that last line. I'm not doing DI for the sake of doing DI because it's cool, I'm doing it because I need some objects injected in certain places. DI is usually expressed in terms of ctor or method injection, which is often impractical for a project of significant size. Carry a reference down thru 15 call levels just so it can be injected in the object that needs it? Add ctor injectors to 500 or 1000 classes? Let's not be ridiculous. What I need is reflective injection....
– Qodex
Mar 22 at 14:40
So, yes, I did some research, I picked a DI library (LightInject), took a shot at it (multiple shots, actually), ran into a problem, and asked a question. In case the main question wasn't clear, it's this: is there a way to get LightInject to do reflective injection? It appeared that it was doing so in a project I no longer have access to. Extensive googling has not turned up an answer. I'm not looking for a recommendation, just an answer to the question, can library X do Y?
– Qodex
Mar 22 at 14:46
So, yes, I did some research, I picked a DI library (LightInject), took a shot at it (multiple shots, actually), ran into a problem, and asked a question. In case the main question wasn't clear, it's this: is there a way to get LightInject to do reflective injection? It appeared that it was doing so in a project I no longer have access to. Extensive googling has not turned up an answer. I'm not looking for a recommendation, just an answer to the question, can library X do Y?
– Qodex
Mar 22 at 14:46
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55285118%2flightinject-and-automatic-property-instantiation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55285118%2flightinject-and-automatic-property-instantiation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Be careful with that last line because that's using the service locator pattern, which can appear to be similar to dependency injection but definitely isn't. What you need to do read up on DI best practices and research libraries. SO doesn't do library / external resource recommendations, so do some research, pick a DI library, take a shot at it, and then ask a question if you run into a problem.
– madreflection
Mar 21 at 16:41
I'm always careful. But the whole point of this questions is to NOT do what's in that last line. I'm not doing DI for the sake of doing DI because it's cool, I'm doing it because I need some objects injected in certain places. DI is usually expressed in terms of ctor or method injection, which is often impractical for a project of significant size. Carry a reference down thru 15 call levels just so it can be injected in the object that needs it? Add ctor injectors to 500 or 1000 classes? Let's not be ridiculous. What I need is reflective injection....
– Qodex
Mar 22 at 14:40
So, yes, I did some research, I picked a DI library (LightInject), took a shot at it (multiple shots, actually), ran into a problem, and asked a question. In case the main question wasn't clear, it's this: is there a way to get LightInject to do reflective injection? It appeared that it was doing so in a project I no longer have access to. Extensive googling has not turned up an answer. I'm not looking for a recommendation, just an answer to the question, can library X do Y?
– Qodex
Mar 22 at 14:46