You can set special conditions for the activation of bindings.
Here are some examples:
public class ConditionTest
{
public string Test { get; set; }
public ConditionTest(string test)
{
Test = test;
}
}
First example:
const string value = "Test";
//Create container.
var injector = new MugenInjector();
//Indicates that the binding should be used only for injections on the specified type.
injector.Bind<string>().ToConstant(value).WhenInto<ConditionTest>();
var conditionTest = injector.Get<ConditionTest>();
if (conditionTest.Test == value)
Console.WriteLine("Is always true");
try
{
injector.Get<string>();
}
catch (Exception)
{
Console.WriteLine("Is always true");
}
Second example:
const string value = "Test";
//Create container.
var injector = new MugenInjector();
//Indicates that the binding should be used only for injections on the specified type when namespace equals.
injector.Bind<string>().ToConstant(value).WhenNamespaceEqual("YOUR NAMESPACE");
var conditionTest = injector.Get<ConditionTest>();
if (conditionTest.Test == value)
Console.WriteLine("Is always true");
try
{
injector.Get<string>();
}
catch (Exception)
{
Console.WriteLine("Is always true");
}
Third example:
const string value = "Test";
//Create container.
var injector = new MugenInjector();
//Indicates that the binding should be used only for requests that support the specified condition.
injector.Bind<string>().ToConstant(value).When(binding => binding.TypeInto == typeof(ConditionTest));
var conditionTest = injector.Get<ConditionTest>();
if (conditionTest.Test == value)
Console.WriteLine("Is always true");
try
{
injector.Get<string>();
}
catch (Exception)
{
Console.WriteLine("Is always true");
}