コード例 #1
0
    public void TestIndexers()
    {
        using (var indexedProperties = new TestIndexedProperties())
        {
            Assert.AreEqual(1, indexedProperties[0]);
            Assert.AreEqual(1, indexedProperties["foo"]);
            indexedProperties[0] = 2;
            Assert.AreEqual(2, indexedProperties[0]);
            indexedProperties[0f] = 3;
            Assert.AreEqual(3, indexedProperties[0f]);
            var properties = indexedProperties[(byte)0];
            Assert.AreEqual(0, properties.Field);
            var newProperties = new TestProperties();
            newProperties.Field        = 4;
            indexedProperties[(byte)0] = newProperties;
            Assert.AreEqual(4, indexedProperties[(byte)0].Field);
            newProperties = indexedProperties[(short)0];
            Assert.AreEqual(4, newProperties.Field);
            newProperties.Field = 5;
            Assert.AreEqual(5, indexedProperties[(byte)0].Field);
            newProperties.get();
            newProperties.set(0);
            newProperties.Get();
            newProperties.Set(0);

            var bar = new Bar {
                A = 5
            };
            indexedProperties[0u] = bar;
            Assert.That(bar.A, Is.EqualTo(indexedProperties[0u].A));
            indexedProperties[(ushort)0] = bar;
            Assert.That(bar.A, Is.EqualTo(indexedProperties[(ushort)0].A));

            using (var foo = new Foo())
            {
                var bar2 = new Bar {
                    A = 10
                };
                indexedProperties[foo] = bar2;
                Assert.That(bar2.A, Is.EqualTo(indexedProperties[foo].A));
            }
        }
    }