public static bool TryParse(string s, out GuidUdi udi) { Udi tmp; udi = null; if (TryParse(s, out tmp) == false) { return(false); } udi = tmp as GuidUdi; return(udi != null); }
private static bool ParseInternal(string s, bool tryParse, bool knownTypes, out Udi udi) { if (knownTypes == false) { EnsureScanForUdiTypes(); } udi = null; Uri uri; if (Uri.IsWellFormedUriString(s, UriKind.Absolute) == false || Uri.TryCreate(s, UriKind.Absolute, out uri) == false) { if (tryParse) { return(false); } throw new FormatException(string.Format("String \"{0}\" is not a valid udi.", s)); } var entityType = uri.Host; UdiType udiType; if (_udiTypes.TryGetValue(entityType, out udiType) == false) { if (knownTypes) { // not knowing the type is not an error // just return the unknown type udi udi = UnknownTypeUdi.Instance; return(false); } if (tryParse) { return(false); } throw new FormatException(string.Format("Unknown entity type \"{0}\".", entityType)); } var path = uri.AbsolutePath.TrimStart('/'); if (udiType == UdiType.GuidUdi) { if (path == string.Empty) { udi = GetRootUdi(uri.Host); return(true); } Guid guid; if (Guid.TryParse(path, out guid) == false) { if (tryParse) { return(false); } throw new FormatException(string.Format("String \"{0}\" is not a valid udi.", s)); } udi = new GuidUdi(uri.Host, guid); return(true); } if (udiType == UdiType.StringUdi) { udi = path == string.Empty ? GetRootUdi(uri.Host) : new StringUdi(uri.Host, Uri.UnescapeDataString(path)); return(true); } if (tryParse) { return(false); } throw new InvalidOperationException(string.Format("Invalid udi type \"{0}\".", udiType)); }