MimeKit.MultipartRelated.IndexOf C# (CSharp) Method

IndexOf() public method

Gets the index of the part matching the specified URI.

Finds the index of the part matching the specified URI, if it exists.

If the URI scheme is "cid", then matching is performed based on the Content-Id header values, otherwise the Content-Location headers are used. If the provided URI is absolute and a child part's Content-Location is relative, then then the child part's Content-Location URI will be combined with the value of its Content-Base header, if available, otherwise it will be combined with the multipart/related part's Content-Base header in order to produce an absolute URI that can be compared with the provided absolute URI.

/// is null. ///
public IndexOf ( Uri uri ) : int
uri Uri The URI of the MIME part.
return int
		public int IndexOf (Uri uri)
		{
			if (uri == null)
				throw new ArgumentNullException ("uri");

			bool cid = uri.IsAbsoluteUri && uri.Scheme.ToLowerInvariant () == "cid";

			for (int index = 0; index < Count; index++) {
				var entity = this[index];

				if (uri.IsAbsoluteUri) {
					if (cid) {
						if (entity.ContentId == uri.AbsolutePath)
							return index;
					} else if (entity.ContentLocation != null) {
						Uri absolute;

						if (!entity.ContentLocation.IsAbsoluteUri) {
							if (entity.ContentBase != null) {
								absolute = new Uri (entity.ContentBase, entity.ContentLocation);
							} else if (ContentBase != null) {
								absolute = new Uri (ContentBase, entity.ContentLocation);
							} else {
								continue;
							}
						} else {
							absolute = entity.ContentLocation;
						}

						if (absolute == uri)
							return index;
					}
				} else if (entity.ContentLocation == uri) {
					return index;
				}
			}

			return -1;
		}

Usage Example

		public void TestArgumentExceptions ()
		{
			var related = new MultipartRelated ();
			string mimeType, charset;

			Assert.Throws<ArgumentNullException> (() => new MultipartRelated ((MimeEntityConstructorArgs) null));
			Assert.Throws<ArgumentNullException> (() => related.Open (null, out mimeType, out charset));
			Assert.Throws<ArgumentNullException> (() => related.Open (null));
			Assert.Throws<ArgumentNullException> (() => related.Contains ((Uri) null));
			Assert.Throws<ArgumentNullException> (() => related.IndexOf ((Uri) null));
			Assert.Throws<ArgumentNullException> (() => related.Accept (null));
			Assert.Throws<ArgumentNullException> (() => related.Root = null);

			Assert.Throws<FileNotFoundException> (() => related.Open (new Uri ("http://www.xamarin.com/logo.png"), out mimeType, out charset));
			Assert.Throws<FileNotFoundException> (() => related.Open (new Uri ("http://www.xamarin.com/logo.png")));
		}
All Usage Examples Of MimeKit.MultipartRelated::IndexOf