c# - How to override a type already registered in LightInject using a new instance passed to the constructor? -


I place some examples in the repository that live throughout the lifetime of my application, but sometimes I would like to Instant replacement is required, even with passing new instances in the Light and Ink container. Gate Instance Constructor Override

Here is a snippet that shows the problem:

  internal square class A {public string name {receives; Private set; } Public class (string name) {name = name; } Public override string toasting () {return name; }} Internal Classes ClassB {Public Class A {get; Private set; } Public Class B (Class A) {A = A; } Public override string toasting () {return string.format ("I {0}", A); }} Private Zero testcontainer () {var Container = New LightInject.ServiceContainer (); Var a1 = new square ("an example 1"); Container. Registration (x = & gt; a1); Container.Register & lt; ClassB & gt; (); Var a2 = new square ("an example 2"); Var bwitha1 = Container.Get instance & lt; Class B & gt; (); If (bvt1a! = 1) {new invalid operation expiation ("this will not happen"); } Var bwitha2 = container.GetInstance & lt; Class, Classby & gt; (A2); If (Bevita 2.A! = A2) {New Invalid Operation Expansion ("Something went wrong here"); }}   

If I give a clear example in GetInstance call, then the pre-registered example takes the LightInject preference? How to get around the problem and build the object with an optional example of logic? Current version of LightInject in

If you want to use runtime logic, you should provide a factory the wanted.

The following solutions can work for you.

  using LightInject; Class program {static zero main (string [] args) {var container = new service container (); Container.Register & lt; Bars & gt; (); Container.Register & lt; Foo & gt; (); Container. Registrar & lt; Bar, foo & gt; ((Factory, Bar) = & gt; New Foo (bar), "Fu WitrintimeAgument"); Var Example = Container.Get instance & lt; Foo & gt; (); Var example WyTrinTMAgram = container.gate instance & lt; Bar, foo & gt; (New bar), "Fu Witrintime Agreement"); }} Public square fu {public fu (repeatedly} {}} public square bar {}    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -