Skip to content

Commit 900ae57

Browse files
committed
Use Cref.TryCreate in DocumentationReference
1 parent e73e114 commit 900ae57

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

src/BitzArt.XDoc/Models/Cref.cs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,25 @@ public Cref(string cref)
1616

1717
var lastIndexOf = cref.LastIndexOf('.');
1818

19-
if (Prefix is "T:")
19+
switch (Prefix)
2020
{
21-
Type = cref.Substring(lastIndexOf + 1, cref.Length - lastIndexOf - 1);
22-
Member = null;
23-
}
24-
else if (Prefix is "M:" or "P:" or "F:")
25-
{
26-
Type = cref.Substring(2, lastIndexOf - 2);
27-
Member = cref.Substring(lastIndexOf + 1, cref.Length - lastIndexOf - 1);
28-
}
29-
else
30-
{
31-
throw new ArgumentException($"Invalid cref: {cref}");
21+
case "T:":
22+
Type = cref.Substring(lastIndexOf + 1, cref.Length - lastIndexOf - 1);
23+
Member = null;
24+
break;
25+
case "M:" or "P:" or "F:":
26+
Type = cref.Substring(2, lastIndexOf - 2);
27+
Member = cref.Substring(lastIndexOf + 1, cref.Length - lastIndexOf - 1);
28+
break;
29+
default:
30+
throw new ArgumentException($"Invalid cref: {cref}");
3231
}
3332

3433
var typeLastIndexOf = Type.LastIndexOf('.');
3534

3635
ShortType = Type.Substring(typeLastIndexOf + 1, Type.Length - typeLastIndexOf - 1);
3736
}
38-
37+
3938
/// <summary>
4039
/// The prefix of the cref (e.g. "T:", "M:", "P:", "F:").
4140
/// </summary>
@@ -64,4 +63,24 @@ public override string ToString()
6463
{
6564
return $"{Prefix}{Type}{(Member != null ? "." + Member : string.Empty)}";
6665
}
66+
67+
/// <summary>
68+
///
69+
/// </summary>
70+
/// <param name="value"></param>
71+
/// <param name="cref"></param>
72+
/// <returns></returns>
73+
public static bool TryCreate(string? value, out Cref? cref)
74+
{
75+
try
76+
{
77+
cref = new Cref(value!);
78+
}
79+
catch
80+
{
81+
cref = null;
82+
}
83+
84+
return cref != null;
85+
}
6786
}

src/BitzArt.XDoc/Models/DocumentationReference.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public class DocumentationReference
2727
/// </summary>
2828
/// <param name="requirementNode"></param>
2929
/// <param name="target"></param>
30-
/// <param name="cref"></param>
31-
public DocumentationReference(XmlNode requirementNode, MemberDocumentation? target, string? cref)
30+
/// <param name="crefValue"></param>
31+
public DocumentationReference(XmlNode requirementNode, MemberDocumentation? target, string? crefValue)
3232
{
3333
Target = target;
3434
RequirementNode = requirementNode;
35-
Cref = string.IsNullOrWhiteSpace(cref) ? null : new Cref(cref);
35+
Cref = Cref.TryCreate(crefValue, out var cref) ? cref : null;
3636
}
3737
}

0 commit comments

Comments
 (0)