コード例 #1
0
        public void TestEnumerators()
        {
            string             path   = Path.Combine(Files.GetExecutingPath(), "Resources\\TestRes.resx");
            ResXResourceReader reader = new ResXResourceReader(path);

            // reading one element, then reset, read first element again
            var resEnumLazy = reader.GetEnumerator();

            resEnumLazy.MoveNext();
            var firstRes = resEnumLazy.Entry;

            resEnumLazy.Reset();
            resEnumLazy.MoveNext();
            Assert.AreEqual(firstRes, resEnumLazy.Entry);

            // getting enumerator again: cached
            var resEnumCached = reader.GetEnumerator();

            Assert.AreNotEqual(resEnumLazy.GetType(), resEnumCached.GetType());
            resEnumCached.MoveNext();
            Assert.AreEqual(firstRes, resEnumCached.Entry);

            // the lazy cached the rest of the elements into a buffer so they both see the second element now
            resEnumLazy.MoveNext();
            resEnumCached.MoveNext();
            Assert.AreEqual(resEnumLazy.Entry, resEnumCached.Entry);

            // getting the metadata returns a cached enumerator now
            var metaEnumCached = reader.GetMetadataEnumerator();

            Assert.AreEqual(resEnumCached.GetType(), metaEnumCached.GetType());

            // as well as alias
            var aliasEnumCached = reader.GetAliasEnumerator();

            Assert.AreEqual(resEnumCached.GetType(), aliasEnumCached.GetType());
            reader.Close();

            // alias enumerators are handled in a special way so they are tested separately
            // reader is recreated to get a lazy enumerator again
            reader = new ResXResourceReader(path);
            var aliasEnumLazy = reader.GetAliasEnumerator();

            aliasEnumLazy.MoveNext();
            var firstAlias = aliasEnumLazy.Entry;

            aliasEnumLazy.Reset();
            aliasEnumLazy.MoveNext();
            Assert.AreEqual(firstAlias, aliasEnumLazy.Entry);

            // getting enumerator again: cached
            aliasEnumCached = reader.GetAliasEnumerator();
            Assert.AreNotEqual(aliasEnumLazy.GetType(), aliasEnumCached.GetType());
            aliasEnumCached.MoveNext();
            Assert.AreEqual(firstAlias, aliasEnumCached.Entry);

            // the lazy cached the rest of the elements into a buffer so they both see the second element now
            aliasEnumLazy.MoveNext();
            aliasEnumCached.MoveNext();
            Assert.AreEqual(aliasEnumLazy.Entry, aliasEnumCached.Entry);

            // normal vs safe mode
            resEnumCached = reader.GetEnumerator();
            resEnumCached.MoveNext();
            Assert.IsNotInstanceOf <ResXDataNode>(resEnumCached.Value);
            reader.SafeMode = true;
            Assert.IsInstanceOf <ResXDataNode>(resEnumCached.Value);

            // however, aliases are always strings
            Assert.IsInstanceOf <string>(aliasEnumCached.Value);
            Assert.IsInstanceOf <string>(aliasEnumLazy.Value);
            reader.SafeMode = false;
            Assert.IsInstanceOf <string>(aliasEnumCached.Value);
            Assert.IsInstanceOf <string>(aliasEnumLazy.Value);

            reader.Close();
        }