/// <summary> /// Gets the value of the specified property of the specified item if the hierarchy supports it, or returns a HRESULT if there was an error. /// </summary> public static int GetProperty <T>(this IVsHierarchy hierarchy, HierarchyId item, VsHierarchyPropID property, T defaultValue, out T result) { Requires.NotNull(hierarchy, nameof(hierarchy)); if (item.IsNilOrEmpty || item.IsSelection) { throw new ArgumentException(null, nameof(item)); } HResult hr = hierarchy.GetProperty(item, (int)property, out object resultObject); if (hr.IsOK) { // NOTE: We consider it a bug in the underlying project system or the caller if this cast fails result = (T)resultObject; return(HResult.OK); } if (hr == VSConstants.DISP_E_MEMBERNOTFOUND) { result = defaultValue; return(HResult.OK); } result = default(T); return(hr); }
/// <summary> /// Gets the value of the specified property of the specified item if the hierarchy supports it, or throws an exception if there was an error. /// </summary> public static T GetProperty <T>(this IVsHierarchy hierarchy, HierarchyId item, VsHierarchyPropID property, T defaultValue = default(T)) { HResult hr = GetProperty(hierarchy, item, property, defaultValue, out T result); if (hr.Failed) { throw hr.Exception; } return(result); }