Mugen Injection [Binding parameters]
The MugenInjector supports register binding with parameters. When you register service you can specify the IInjectionParameter, available parameter types:
- ConstructorParameter - represents the value of a constructor argument.
- PropertyParameter - represents the value of a property parameter.
- MethodParameter - represents the value of a method parameter.
This code shows how to add parameters to binding:
public class Alpha { [Inject] public string PublicField; [Inject] private string _field; [Inject] public string Property { get; set; } public Alpha(string st) { } [Inject] public void Method(string param) { } } // Create your MugenInjector. _injector = new MugenInjector(); _injector.Bind<Alpha>() .ToSelf() .WithConstructorArguments(context => new Alpha("constructorParam")) .WithPropertyValue(alpha => alpha.Property, "propertyParam") .WithMethodArguments((context, alpha) => alpha.Method("methodParam")) .WithParameter(new FieldParameter("_field", "privateFieldParam")); var alpha1 = _injector.Get<Alpha>();
// Create your MugenInjector. _injector = new MugenInjector(); _injector.Bind<Alpha>() .ToSelf() .WithConstructorArguments(context => new Alpha(context.Resolve<string>())) .WithMethodArguments((context, alpha) => alpha.Method(context.Resolve<string>())); _injector.Get<Alpha>();