Пример #1
0
        public void When_creating_a_new_keyedCollectionEx()
        {
            var personA = new Person {
                Name = "A", Age = 1
            };
            var personB = new Person {
                Name = "B", Age = 2
            };
            var personC = new Person {
                Name = "C", Age = 3
            };

            var collection = new KeyedCollectionEx <string, Person>(p => p.Name)
            {
                personA,
                personB
            };

            collection.ShouldNotBeNull();
            collection.Count.ShouldBe(2);
            collection.Contains("A").ShouldBeTrue();
            collection.Contains("B").ShouldBeTrue();
            collection.Contains("C").ShouldBeFalse();

            collection.Contains(personA).ShouldBeTrue();
            collection.Contains(personB).ShouldBeTrue();
            collection.Contains(personC).ShouldBeFalse();

            collection.Add(personC);
            collection.Contains("C").ShouldBeTrue();
            collection.Contains(personC).ShouldBeTrue();

            var dodgyPerson = new Person {
                Name = "A", Age = 666
            };

            Should.Throw <ArgumentException>(() => collection.Add(dodgyPerson)).Message
            .ShouldBe("An item with the same key has already been added.");

            collection["A"].Age.ShouldBe(1);
            collection["B"].Age.ShouldBe(2);
            collection["C"].Age.ShouldBe(3);

            Person somePerson;

            collection.TryGet("A", out somePerson).ShouldBeTrue();
            somePerson.ShouldNotBeNull();
            somePerson.Age.ShouldBe(1);

            collection.TryGet("D", out somePerson).ShouldBeFalse();
            somePerson.ShouldBeNull();
        }
Пример #2
0
        public Script CreateNewScript(string _name, string _source, string _tagStr, bool _enable = true, bool overwrite = true)
        {
            if (_name.Equals(string.Empty))
            {
                return(null);
            }
            if (Scripts.Contains(_name))
            {
                if (overwrite)
                {
                    Scripts.Remove(_name);
                }
                else
                {
                    return(null);
                }
            }

            Script t = new Script(_name, _source, _tagStr, _enable);

            t.Relink();
            Add(t);
            return(t);
        }