A few examples of work with parameters. To add special parameters in Binding you can use this syntax:

    public class Test : ITest
    {
        public string St { get; set; }

        public Test(string st)
        {
            St = st;
        }

        public Test()
        {
            
        }
    }

//Somewhere in code
const string value = "Test";
var injector = new MugenInjector();
injector.Bind<Test>().ToSelf().WithConstructorArgument("st", value);
var test = injector.Get<Test>();
if (test.St == value)
    Console.WriteLine("It always true");

In this example, the constructor will be selected in accordance with the parameter. 

Parameters can also be passed at creation time, the constructor will be selected on the basis of parameters.

            const string value = "Test";
            var injector = new MugenInjector();
            injector.Bind<Test>().ToSelf();
            var test = injector.Get<Test>();
            if (test.St != value)
                Console.WriteLine("It always true");

            test = injector.Get<Test>(new ConstructorParameter<string>("st", value));
            if (test.St == value)
                Console.WriteLine("It always true");

If the platform supports Expression, you can use this syntax:

    public interface IEmpty { }
    public class Empty : IEmpty { }

    public class Test
    {
        public string St { get; set; }
        public IEmpty Empty { get; set; }

        public Test(string st, IEmpty empty)
        {
            St = st;
            Empty = empty;
        }

        public Test()
        {

        }
    }

            const string value = "Test";
            var injector = new MugenInjector();
            injector.Bind().To();
            injector.Bind().To(context => new Test(value, context.Resolve()));
            var test = injector.Get();
            if (test.St == value)
                Console.WriteLine("It always true");

            const string value1 = "Test1";
            test = injector.Get(context => new Test(value1, context.Resolve()));
            if (test.St == value1)
                Console.WriteLine("It always true");

Notice that the Expression is only for the parameters, not to activate the object.

To pass a parameter to a method, field or property, you need to put the attribute on the field, method, or property specified in the settings IInjector (the default is InjectAttribute). You can change this behavior by implementing the interface IActivator.

    public interface IEmpty { }
    public class Empty : IEmpty { }

    public class Test
    {
        public string MethodParameter;

        [Inject]
        public IEmpty Field;

        [Inject]
        public string Property { get; set; }

        [Inject]
        public void Method(string parameter)
        {
            MethodParameter = parameter;
        }
    }

            const string value = "Test";
            const string propValueFromBinding = "propValueFromBinding";
            var injector = new MugenInjector();
            var empty = new Empty();
            injector.Bind<IEmpty>().ToConstant(empty);
            injector.Bind<string>().ToConstant(value);
            injector.Bind<Test>().ToSelf().WithPropertyValue("Property", propValueFromBinding);
            
            var test = injector.Get<Test>();
            if (test.Field == empty)
                Console.WriteLine("It always true");
            if (test.Property == propValueFromBinding)
                Console.WriteLine("It always true");
            if(test.MethodParameter == value)
                Console.WriteLine("It always true");


            var newEmpty = new Empty();
            const string newValue = "newValue";
            test = injector.Get<Test>(new FieldParameter<IEmpty>("Field", newEmpty),
                               new MethodParameter<string>("Method", "parameter", newValue),
                               new PropertyParameter<string>("Property", newValue));
            if (test.Field == newEmpty)
                Console.WriteLine("It always true");
            if (test.Property == newValue)
                Console.WriteLine("It always true");
            if (test.MethodParameter == newValue)
                Console.WriteLine("It always true");

The parameters you pass when creating an object will always be more important than other parameters.

Last edited Apr 4, 2012 at 5:34 PM by VVS0205, version 4

Comments

No comments yet.