Ejemplo n.º 1
0
        public void Explicit_model_binder_binds_only_to_configured_ctor_parameters()
        {
            var intOption = new Option("-a")
            {
                Argument = new Argument <int>()
            };
            var stringOption = new Option("-b")
            {
                Argument = new Argument <string>()
            };
            var parser = new Parser(intOption, stringOption);
            var ctor   = typeof(ClassWithMultiLetterCtorParameters)
                         .GetConstructors(BindingFlags.Public | BindingFlags.Instance)
                         .First();
            var paramInfo = ctor.GetParameters().First();

            var bindingContext = new BindingContext(parser.Parse("-a 42 -b Hello"));
            var binder         = new ModelBinder <ClassWithMultiLetterCtorParameters>
            {
                EnforceExplicitBinding = true
            };

            binder.BindConstructorArgumentFromValue(paramInfo, intOption);
            var instance = binder.CreateInstance(bindingContext) as ClassWithMultiLetterCtorParameters;

            instance.Should().NotBeNull();
            instance.IntOption.Should().Be(42);
            instance.StringOption.Should().Be("the default");
        }