Exemplo n.º 1
0
            public void AddType(Type type)
            {
                var defaultConstructor = type.GetConstructor(
                    BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
                    null,
                    Type.EmptyTypes,
                    null);

                if (defaultConstructor == null)
                {
                    return;
                }

                if (_types.Any())
                {
                    _factory.Switch(nameof(StateErrorMultipleTypes));
                }
                else
                {
                    if (defaultConstructor.IsPublic)
                    {
                        var lambda = Expression.Lambda <Func <object> >(Expression.New(type)).Compile();
                        _factory.RegisterAndSwitch(string.Empty, lambda);
                    }
                    else
                    {
                        _factory.RegisterAndSwitch(string.Empty, () => defaultConstructor.Invoke(null));
                    }
                }
                _types.Add(type);
            }
Exemplo n.º 2
0
        public void Switcher_DelegateSwitch_Success()
        {
            var swithcer = new DelegateSwitcher <Func <int> >();

            Assert.IsNull(swithcer.Active);
            swithcer.Register(1, () => 1);
            Assert.IsNull(swithcer.Active);
            swithcer.Switch(1);
            Assert.IsNotNull(swithcer.Active);
            Assert.AreEqual(1, swithcer.Active());
        }