当前位置: 首页>>代码示例>>C#>>正文


C# File.AddReferences方法代码示例

本文整理汇总了C#中File.AddReferences方法的典型用法代码示例。如果您正苦于以下问题:C# File.AddReferences方法的具体用法?C# File.AddReferences怎么用?C# File.AddReferences使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在File的用法示例。


在下文中一共展示了File.AddReferences方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Storage2_Milestone4_UseReferences

		public void Storage2_Milestone4_UseReferences()
		{
			var fileName = "Storage2_Milestone2_TestFile.txt";
			var filePath = RepositoryPath.Combine(TestRoot.Path, fileName);
			var text = "Lorem ipsum dolor sit amet...";

			if(ContentType.GetByName("File2") == null)
				ContentTypeInstaller.InstallContentType(@"<?xml version=""1.0"" encoding=""utf-8""?>
					<ContentType name=""File2"" parentType=""File"" handler=""SenseNet.ContentRepository.File"" xmlns=""http://schemas.sensenet.com/SenseNet/ContentRepository/ContentTypeDefinition"">
						<DisplayName>Folder</DisplayName>
						<Description>Use folders to group information to one place</Description>
						<Icon>Folder</Icon>
						<Fields>
							<Field name=""References"" type=""Reference"" />
						</Fields>
					</ContentType>");

			int rootId = TestRoot.Id;
			int file2ctId = ContentType.GetByName("File2").Id;
			int systemId = Node.LoadNode("/Root/System").Id;

			if (Node.Exists(filePath))
                Node.ForceDelete(filePath);

			//----------------------------- Create

			var file = new File(TestRoot, "File2");
			file.Name = fileName;
			file.GetBinary("Binary").FileName = fileName;
			file.GetBinary("Binary").SetStream(Tools.GetStreamFromString(text));
			file.AddReferences("References", new Node[] { TestRoot, ContentType.GetByName("File2") });

			file.Save();

			//----------------------------- Load

			var loadedNode = Node.Load<File>(filePath);
			var loadedStream = loadedNode.GetBinary("Binary").GetStream();
			var loadedText = Tools.GetStreamString(loadedStream);
			var loadedReferences = loadedNode.GetReferences("References").ToList<Node>();

			Assert.IsTrue(loadedText == text, "#1");
			Assert.IsTrue(loadedReferences.Count == 2, "#2");
			var id0 = loadedReferences[0].Id;
			var id1 = loadedReferences[1].Id;
			Assert.IsTrue(id0 == rootId || id0 == file2ctId, "#3");
			Assert.IsTrue(id1 == rootId || id1 == file2ctId, "#4");

			//----------------------------- Change references

			var refs = ((IEnumerable<Node>)loadedNode["References"]).ToList();
			refs.Remove(TestRoot);

			Assert.IsTrue(refs.Count == 2, "#5");

            loadedNode.RemoveReference("References", TestRoot);
            refs = ((IEnumerable<Node>)loadedNode["References"]).ToList();

			Assert.IsTrue(refs.Count == 1, "#6");
			id0 = refs[0].Id;
			Assert.IsTrue(id0 == file2ctId, "#7");

			refs.Add(Node.LoadNode("/Root/System"));

            loadedNode.SetReferences("References", refs);
            refs = ((IEnumerable<Node>)loadedNode["References"]).ToList();

			Assert.IsTrue(refs.Count == 2, "#8");
			id0 = refs[0].Id;
			id1 = refs[1].Id;
			Assert.IsTrue(id0 == file2ctId, "#9");
			Assert.IsTrue(id1 == systemId, "#10");

			loadedNode.Save();

			//----------------------------- Reload

			var reloadedNode = Node.Load<File>(filePath);
			var reloadedStream = reloadedNode.GetBinary("Binary").GetStream();
			var reloadedText = Tools.GetStreamString(reloadedStream);
			var reloadedReferences = reloadedNode.GetReferences("References").ToList<Node>();

			Assert.IsTrue(reloadedText == text, "#11");
			Assert.IsTrue(reloadedReferences.Count == 2, "#12");
			id0 = reloadedReferences[0].Id;
			id1 = reloadedReferences[1].Id;
            Assert.IsTrue(id0 != id1, "#13");
            Assert.IsTrue(id0 == file2ctId || id0 == systemId, "#14");
            Assert.IsTrue(id1 == file2ctId || id1 == systemId, "#15");

			//----------------------------- Reload

            var identityCheck = reloadedNode.GetReferences("References");
            var countBefore = identityCheck.ToArray().Length;
            reloadedNode.AddReference("References", Repository.Root);
            var countAfter = identityCheck.ToArray().Length;
            Assert.IsTrue(countAfter == countBefore + 1, "#15");
		}
开发者ID:maxpavlov,项目名称:FlexNet,代码行数:98,代码来源:NodeTest.cs


注:本文中的File.AddReferences方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。