Beispiel #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);
            }
Beispiel #2
0
        public void Switcher_SwitchingPredicate_Success()
        {
            var swithcer = new DelegateSwitcher <Func <int> >(key => !Equals(key, 3));

            swithcer.RegisterAndSwitch(1, () => 1);
            Assert.AreEqual(1, swithcer.Active());
            swithcer.RegisterAndSwitch(2, () => 2);
            Assert.AreEqual(2, swithcer.Active());
            swithcer.RegisterAndSwitch(3, () => 3);
            Assert.AreEqual(2, swithcer.Active());
        }
Beispiel #3
0
        public void Switcher_DelegateRegisterAndSwitch_Success()
        {
            var swithcer = new DelegateSwitcher <Func <int> >();

            Assert.IsNull(swithcer.Active);
            swithcer.RegisterAndSwitch(1, () => 1);
            Assert.IsNotNull(swithcer.Active);
            Assert.AreEqual(1, swithcer.Active());
        }
Beispiel #4
0
 public ExpressionTypeCache(string root)
 {
     if (root == null)
     {
         throw new ArgumentNullException(nameof(root));
     }
     _root    = root;
     _types   = new List <Type>();
     _factory = new DelegateSwitcher <Func <object> >();
     _factory.RegisterAndSwitch(nameof(StateDefault), StateDefault);
     _factory.Register(nameof(StateErrorMultipleTypes), StateErrorMultipleTypes);
 }