Skip to content

Commit af15bab

Browse files
Added extension method
1 parent c569257 commit af15bab

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/Community.VisualStudio.Toolkit.Shared/ExtensionMethods/IVsHierarchyExtensions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,27 @@ namespace Microsoft.VisualStudio.Shell.Interop
99
/// </summary>
1010
public static class IVsHierarchyExtensions
1111
{
12+
/// <summary>
13+
/// Tries to get a property from a hierarchy item.
14+
/// </summary>
15+
/// <remarks>
16+
/// Inspired by https://github.com/dotnet/roslyn/blob/main/src/VisualStudio/Core/Def/Implementation/ProjectSystem/Extensions/IVsHierarchyExtensions.cs
17+
/// </remarks>
18+
public static bool TryGetItemProperty<T>(this IVsHierarchy hierarchy, uint itemId, int propertyId, out T? value)
19+
{
20+
ThreadHelper.ThrowIfNotOnUIThread();
21+
22+
if (ErrorHandler.Failed(hierarchy.GetProperty(itemId, propertyId, out var property)) || !(property is T))
23+
{
24+
value = default;
25+
return false;
26+
}
27+
28+
value = (T)property;
29+
30+
return true;
31+
}
32+
1233
/// <summary>
1334
/// Converts an IVsHierarchy to a Project.
1435
/// </summary>

0 commit comments

Comments
 (0)